473,382 Members | 1,225 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Python program to sort lists

thatos
105 100+
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.
Expand|Select|Wrap|Line Numbers
  1. >>>def short(list2):
  2.          while len(list2) != 0:
  3.             new = []
  4.             loop = -1
  5.             a = min(list2)
  6.             for s in list2:
  7.               loop += 1
  8.               if s == a:
  9.                 new.append(a)
  10.                 list.pop(loop)
  11.                 return list2
Sep 16 '07 #1
4 1744
ilikepython
844 Expert 512MB
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.
Sep 16 '07 #2
bartonc
6,596 Expert 4TB
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.
Expand|Select|Wrap|Line Numbers
  1. >>>def short(list2):
  2.          while len(list2) != 0:
  3.             new = []
  4.             loop = -1
  5.             a = min(list2)
  6.             for s in list2:
  7.               loop += 1
  8.               if s == a:
  9.                 new.append(a)
  10.                 list.pop(loop)
  11.                 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.
Expand|Select|Wrap|Line Numbers
  1. def short(list2):
  2.     newList = []  # do this outside to loop
  3.     while len(list2) != 0:
  4.         loop = -1
  5.         a = min(list2)
  6.         for s in list2:
  7.             loop += 1
  8.             if s == a:
  9.                 newList.append(a)
  10.                 list2.pop(loop)
  11.     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.
Sep 16 '07 #3
thatos
105 100+
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.
Expand|Select|Wrap|Line Numbers
  1. def short(list2):
  2.     newList = []  # do this outside to loop
  3.     while len(list2) != 0:
  4.         loop = -1
  5.         a = min(list2)
  6.         for s in list2:
  7.             loop += 1
  8.             if s == a:
  9.                 newList.append(a)
  10.                 list2.pop(loop)
  11.     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]
Sep 16 '07 #4
thatos
105 100+
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.
Expand|Select|Wrap|Line Numbers
  1. def short(list2):
  2.     newList = []  # do this outside to loop
  3.     while len(list2) != 0:
  4.         loop = -1
  5.         a = min(list2)
  6.         for s in list2:
  7.             loop += 1
  8.             if s == a:
  9.                 newList.append(a)
  10.                 list2.pop(loop)
  11.     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
Sep 16 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

220
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...
54
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...
226
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...
7
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....
44
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...
68
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
34
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...
0
isladogs
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...
0
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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...
0
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
0
BarryA
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...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.