如何以数字方式对列表进行排序?

时间:2022-04-15 07:42:37

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.

我知道这听起来微不足道,但我没有意识到Python的sort()函数很奇怪。我有一个实际上是字符串形式的“数字”列表,所以我首先将它们转换为int,然后尝试排序。

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.

我知道这可能是一个毫无疑问的问题,但谷歌和我的教科书没有提供比.sort()函数更多或更少有用的东西。

9 个解决方案

#1


140  

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)

但是,python使你更容易:sort接受一个命名参数key,这是一个在比较每个元素之前调用的函数(但不修改列表)

list1 = ["1","10","3","22","23","4","2","200"]
# call int(x) on each element before comparing it
list1.sort(key=int)

#2


30  

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.

您可以将函数传递给.sort方法的key参数。有了这个,系统将按键(x)而不是x排序。

list1.sort(key=int)

BTW, to convert the list to integers permanently, use the map function

顺便说一句,要将列表永久转换为整数,请使用map函数

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


13  

In case you want to use sorted() function: sorted(list1, key=int)

如果你想使用sorted()函数:sorted(list1,key = int)

It returns a new sorted list.

它返回一个新的排序列表。

#4


10  

Python's sort isn't weird. It's just that this code:

Python的排序并不奇怪。只是这段代码:

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.

无论如何,正确的解决方案是使用key = int,正如其他人向您展示的那样。

#5


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..]

这与你可以在互联网上找到的其他东西非常相似,但也适用于像[abc0.1,abc0.2 ..]这样的字母数字

#6


3  

Seamus Campbell's answer doesnot work on python2.x.
list1 = sorted(list1, key=lambda e: int(e)) using lambda function works well.

Seamus Campbell的答案对python2.x不起作用。 list1 = sorted(list1,key = lambda e:int(e))使用lambda函数效果很好。

#7


0  

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.

最新的解决方案是正确的。您正在以字符串形式阅读解决方案,在这种情况下,顺序为1,然后是100,然后是104,然后是2,然后是21,然后是2001001010,3,依此类推。

You have to CAST your input as an int instead:

你必须将你的输入作为int来代替:

sorted strings:

排序字符串:

stringList = (1, 10, 2, 21, 3)

stringList =(1,10,2,21,3)

sorted ints:

排序的整数:

intList = (1, 2, 3, 10, 21)

intList =(1,2,3,10,21)

To cast, just put the stringList inside int ( blahblah ).

要转换,只需将stringList放在int(blahblah)中。

Again:

再次:

stringList = (1, 10, 2, 21, 3)

newList = int (stringList)

print newList

=> returns (1, 2, 3, 10, 21) 

#8


0  

I approached the same problem yesterday and found a module called natsort which solves the problems. Use:

我昨天遇到了同样的问题,发现了一个名为natsort的模块来解决问题。使用:

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']

#9


-2  

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.

这对我来说使用python版本3,虽然它没有在版本2中。

#1


140  

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)

但是,python使你更容易:sort接受一个命名参数key,这是一个在比较每个元素之前调用的函数(但不修改列表)

list1 = ["1","10","3","22","23","4","2","200"]
# call int(x) on each element before comparing it
list1.sort(key=int)

#2


30  

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.

您可以将函数传递给.sort方法的key参数。有了这个,系统将按键(x)而不是x排序。

list1.sort(key=int)

BTW, to convert the list to integers permanently, use the map function

顺便说一句,要将列表永久转换为整数,请使用map函数

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


13  

In case you want to use sorted() function: sorted(list1, key=int)

如果你想使用sorted()函数:sorted(list1,key = int)

It returns a new sorted list.

它返回一个新的排序列表。

#4


10  

Python's sort isn't weird. It's just that this code:

Python的排序并不奇怪。只是这段代码:

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.

无论如何,正确的解决方案是使用key = int,正如其他人向您展示的那样。

#5


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..]

这与你可以在互联网上找到的其他东西非常相似,但也适用于像[abc0.1,abc0.2 ..]这样的字母数字

#6


3  

Seamus Campbell's answer doesnot work on python2.x.
list1 = sorted(list1, key=lambda e: int(e)) using lambda function works well.

Seamus Campbell的答案对python2.x不起作用。 list1 = sorted(list1,key = lambda e:int(e))使用lambda函数效果很好。

#7


0  

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.

最新的解决方案是正确的。您正在以字符串形式阅读解决方案,在这种情况下,顺序为1,然后是100,然后是104,然后是2,然后是21,然后是2001001010,3,依此类推。

You have to CAST your input as an int instead:

你必须将你的输入作为int来代替:

sorted strings:

排序字符串:

stringList = (1, 10, 2, 21, 3)

stringList =(1,10,2,21,3)

sorted ints:

排序的整数:

intList = (1, 2, 3, 10, 21)

intList =(1,2,3,10,21)

To cast, just put the stringList inside int ( blahblah ).

要转换,只需将stringList放在int(blahblah)中。

Again:

再次:

stringList = (1, 10, 2, 21, 3)

newList = int (stringList)

print newList

=> returns (1, 2, 3, 10, 21) 

#8


0  

I approached the same problem yesterday and found a module called natsort which solves the problems. Use:

我昨天遇到了同样的问题,发现了一个名为natsort的模块来解决问题。使用:

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']

#9


-2  

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.

这对我来说使用python版本3,虽然它没有在版本2中。