When I run the code in only returns the new list with only the min of the list it doesn't go throughtout the list to return other numbers in the list. -
>>>def short(list2):
-
while len(list2) != 0:
-
new = []
-
loop = -1
-
a = min(list2)
-
for s in list2:
-
loop += 1
-
if s == a:
-
new.append(a)
-
list.pop(loop)
-
return list2
4 1744
When I run the code in only returns the new list with only the min of the list it doesn't go throughtout the list to return other numbers in the list.
>>>def short(list2):
while len(list2) != 0:
new = []
loop = -1
a = min(list2)
for s in list2:
loop += 1
if s == a:
new.append(a)
list.pop(loop)
return list2
Why are you doing this? There is a built in sorted() function that sorts an itarable and lists have a list.sort() function which sorts in place.
When I run the code in only returns the new list with only the min of the list it doesn't go throughtout the list to return other numbers in the list. -
>>>def short(list2):
-
while len(list2) != 0:
-
new = []
-
loop = -1
-
a = min(list2)
-
for s in list2:
-
loop += 1
-
if s == a:
-
new.append(a)
-
list.pop(loop)
-
return list2
The problem here is (and I believe that writing sort routines is very good practice) that you hit the return inside the inner loop. You don't want to return until the whole sort is complete: Also, you are continually making a new, empty list inside the while loop. -
def short(list2):
-
newList = [] # do this outside to loop
-
while len(list2) != 0:
-
loop = -1
-
a = min(list2)
-
for s in list2:
-
loop += 1
-
if s == a:
-
newList.append(a)
-
list2.pop(loop)
-
return newList
Note, however, that a list sent into this routine will end up empty due to the "pass-by-reference" nature of Python and most other programming languages.
The problem here is (and I believe that writing sort routines is very good practice) that you hit the return inside the inner loop. You don't want to return until the whole sort is complete: Also, you are continually making a new, empty list inside the while loop. -
def short(list2):
-
newList = [] # do this outside to loop
-
while len(list2) != 0:
-
loop = -1
-
a = min(list2)
-
for s in list2:
-
loop += 1
-
if s == a:
-
newList.append(a)
-
list2.pop(loop)
-
return newList
Note, however, that a list sent into this routine will end up empty due to the "pass-by-reference" nature of Python and most other programming languages.
when I want to sort [3,2,1] it return [1,1,3]
The problem here is (and I believe that writing sort routines is very good practice) that you hit the return inside the inner loop. You don't want to return until the whole sort is complete: Also, you are continually making a new, empty list inside the while loop. -
def short(list2):
-
newList = [] # do this outside to loop
-
while len(list2) != 0:
-
loop = -1
-
a = min(list2)
-
for s in list2:
-
loop += 1
-
if s == a:
-
newList.append(a)
-
list2.pop(loop)
-
return newList
Note, however, that a list sent into this routine will end up empty due to the "pass-by-reference" nature of Python and most other programming languages.
thanks for your help you helped me a lot and at least I'm improving
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Brandon J. Van Every |
last post by:
What's better about Ruby than Python? I'm sure there's something. What is
it?
This is not a troll. I'm language shopping and I want people's answers. I
don't know beans about Ruby or have...
|
by: Brandon J. Van Every |
last post by:
I'm realizing I didn't frame my question well.
What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump
up in your chair and scream "Wow! Ruby has *that*? That is SO...
|
by: Stephen C. Waterbury |
last post by:
This seems like it ought to work, according to the
description of reduce(), but it doesn't. Is this
a bug, or am I missing something?
Python 2.3.2 (#1, Oct 20 2003, 01:04:35)
on linux2
Type...
|
by: svilen |
last post by:
hello again.
i'm now into using python instead of another language(s) for
describing structures of data, including names, structure,
type-checks, conversions, value-validations, metadata etc....
|
by: Iwan van der Kleyn |
last post by:
Please ignore if you are allergic to ramblings :-)
Despite a puritan streak I've always tried to refrain from language wars
or syntax bickering; call it enforced pragmatism. That's the main...
|
by: Lad |
last post by:
Is anyone capable of providing Python advantages over PHP if there are
any?
Cheers,
L.
|
by: Anthony Irwin |
last post by:
Hi All,
I am currently trying to decide between using python or java and have
a few quick questions about python that you may be able to help with.
#1 Does python have something like javas...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |