可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I know that this sounds trivial but I did not realize that the sort()
function of Python was weird. I have a list of \"numbers\" that are actually in string form, so I first convert them to ints, then attempt a sort.
list1=[\"1\",\"10\",\"3\",\"22\",\"23\",\"4\",\"2\",\"200\"]
for item in list1:
item=int(item)
list1.sort()
print list1
Gives me:
[\'1\', \'10\', \'2\', \'200\', \'22\', \'23\', \'3\', \'4\']
What I want is
[\'1\',\'2\',\'3\',\'4\',\'10\',\'22\',\'23\',\'200\']
I\'ve looked around for some of the algorithms associated with sorting numeric sets, but the ones I found all involve sorting alphanumeric sets.
I know this is probably a no brainer problem but google and my textbook don\'t offer anything more or less useful than the .sort()
function.
回答1:
You haven\'t actually converted your strings to ints. Or rather, you did, but then you didn\'t do anything with the results. What you want is:
list1 = [\"1\",\"10\",\"3\",\"22\",\"23\",\"4\",\"2\",\"200\"]
list1 = [int(x) for x in list1]
list1.sort()
However, python makes it even easier for you: sort takes a named parameter, key, which is a function that is called on each element before it is compared (but without modifying the list)
list1 = [\"1\",\"10\",\"3\",\"22\",\"23\",\"4\",\"2\",\"200\"]
# call int(x) on each element before comparing it
list1.sort(key=int)
回答2:
You could pass a function to the key
parameter to the .sort
method. With this, the system will sort by key(x) instead of x.
list1.sort(key=int)
BTW, to convert the list to integers permanently, use the map
function
list1 = list(map(int, list1)) # you don\'t need to call list() in Python 2.x
or list comprehension
list1 = [int(x) for x in list1]
回答3:
In case you want to use sorted()
function: sorted(list1, key=int)
It returns a new sorted list.
回答4:
Python\'s sort isn\'t weird. It\'s just that this code:
for item in list1:
item=int(item)
isn\'t doing what you think it is - item
is not replaced back into the list, it is simply thrown away.
Anyway, the correct solution is to use key=int
as others have shown you.
回答5:
You can also use:
import re
def sort_human(l):
convert = lambda text: float(text) if text.isdigit() else text
alphanum = lambda key: [ convert(c) for c in re.split(\'([-+]?[0-9]*\\.?[0-9]*)\', key) ]
l.sort( key=alphanum )
return l
this is very similar for other stuff that you can find on the internet but also works for alphanumericals like [abc0.1, abc0.2..]
回答6:
Seamus Campbell\'s answer doesnot work on python2.x.
list1 = sorted(list1, key=lambda e: int(e))
using lambda
function works well.
回答7:
Try this, it’ll sort the list in-place in descending order (there’s no need to specify a key in this case):
Process
listB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
listC = sorted(listB, reverse=True) # listB remains untouched
print listC
output:
[48, 46, 25, 24, 22, 13, 8, -9, -15, -36]
回答8:
The most recent solution is right. You are reading solutions as a string, in which case the order is 1, then 100, then 104 followed by 2 then 21, then 2001001010, 3 and so forth.
You have to CAST your input as an int instead:
sorted strings:
stringList = (1, 10, 2, 21, 3)
sorted ints:
intList = (1, 2, 3, 10, 21)
To cast, just put the stringList inside int ( blahblah ).
Again:
stringList = (1, 10, 2, 21, 3)
newList = int (stringList)
print newList
=> returns (1, 2, 3, 10, 21)
回答9:
I approached the same problem yesterday and found a module called natsort
which solves the problems. Use:
from natsort import natsorted
# Example list of strings
a = [\'1\', \'10\', \'2\', \'3\', \'11\']
[In] sorted(a)
[Out] [\'1\', \'10\', \'11\', \'2\', \'3\']
[In] natsorted(a)
[Out] [\'1\', \'2\', \'3\', \'10\', \'11\']
回答10:
Simple way to sort a numerical list
numlists = [5,50,7,51,87,97,53]
numlists.sort(reverse=False)
print(numlists)
回答11:
if you want to use string of the numbers better take another list as shown in my code it will work fine.
list1=[\"1\",\"10\",\"3\",\"22\",\"23\",\"4\",\"2\",\"200\"]
k=[]
for item in list1:
k.append(int(item))
k.sort()
print(k)
回答12:
scores = [\'91\',\'89\',\'87\',\'86\',\'85\']
scores.sort()
print (scores)
This worked for me using python version 3, though it didn\'t in version 2.