问题:如何对字符串列表进行数字排序?

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

list1=["1","10","3","22","23","4","2","200"]
for item in list1:
    item=int(item)

list1.sort()
print list1

给我:

['1', '10', '2', '200', '22', '23', '3', '4']

我想要的是

['1','2','3','4','10','22','23','200']

我四处寻找与排序数字集相关的算法,但是我发现所有算法都涉及对字母数字集进行排序。

我知道这可能是个没有脑子的问题,但是google和我的教科书没有提供比该.sort()功能有用的功能。

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.


回答 0

您实际上尚未将字符串转换为int。或更确切地说,您做了,但是随后您对结果什么也没做。您想要的是:

list1 = ["1","10","3","22","23","4","2","200"]
list1 = [int(x) for x in list1]
list1.sort()

如果由于某种原因需要保留字符串而不是整数(通常是一个坏主意,但是可能需要保留前导零或其他东西),则可以使用函数。sort接受一个命名参数,key该参数是在比较每个元素之前对其进行调用的函数。比较键函数的返回值,而不是直接比较列表元素:

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

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()

If for some reason you need to keep strings instead of ints (usually a bad idea, but maybe you need to preserve leading zeros or something), you can use a key function. sort takes a named parameter, key, which is a function that is called on each element before it is compared. The key function’s return values are compared instead of comparing the list elements directly:

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

回答 1

你可以传递一个函数的key参数.sort方法。这样,系统将按key(x)而不是x进行排序。

list1.sort(key=int)

顺便说一句,到列表中,以永久的整数转换,使用map功能

list1 = list(map(int, list1))   # you don't need to call list() in Python 2.x

或列表理解

list1 = [int(x) for x in list1]

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]

回答 2

如果要使用功能:sorted(list1, key=int)

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

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

It returns a new sorted list.


回答 3

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

for item in list1:
   item=int(item)

没有按照您的想法去做- item不会被替换回列表中,只是被丢弃了。

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

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.


回答 4

您还可以使用:

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

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

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 to other stuff that you can find on the internet but also works for alphanumericals like [abc0.1, abc0.2, ...].


回答 5

Seamus Campbell的答案不适用于python2.x。
list1 = sorted(list1, key=lambda e: int(e))使用lambda功能效果很好。

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


回答 6

昨天我也遇到了同样的问题,并找到了一个名为[natsort] [1]的模块,它可以解决您的问题。用:

from natsort import natsorted # pip install natsort

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

# Your array may contain strings
[In]  natsorted(['string11', 'string3', 'string1', 'string10', 'string100'])
[Out] ['string1', 'string3', 'string10', 'string11', 'string100']

它也适用于字典sorted。[1]:https//pypi.org/project/natsort/

I approached the same problem yesterday and found a module called [natsort][1], which solves your problem. Use:

from natsort import natsorted # pip install natsort

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

# Your array may contain strings
[In]  natsorted(['string11', 'string3', 'string1', 'string10', 'string100'])
[Out] ['string1', 'string3', 'string10', 'string11', 'string100']

It also works for dictionaries as an equivalent of sorted. [1]: https://pypi.org/project/natsort/


回答 7

尝试此操作,它将按降序对列表进行排序(在这种情况下,无需指定键):

处理

listB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
listC = sorted(listB, reverse=True) # listB remains untouched
print listC

输出:

 [48, 46, 25, 24, 22, 13, 8, -9, -15, -36]

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

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

您必须将输入的内容转换为int:

排序的字符串:

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

排序的整数:

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

要进行转换,只需将stringList放入int(blahblah)中。

再次:

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

newList = int (stringList)

print newList

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

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

如果您想使用数字字符串更好地采用我的代码中所示的另一个列表,它将很好地工作。

list1=["1","10","3","22","23","4","2","200"]

k=[]    
for item in list1:    
    k.append(int(item))

k.sort()
print(k)
# [1, 2, 3, 4, 10, 22, 23, 200]

If you want to use strings 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)
# [1, 2, 3, 4, 10, 22, 23, 200]

回答 10

排序数字列表的简单方法

numlists = ["5","50","7","51","87","97","53"]
results = list(map(int, numlists))
results.sort(reverse=False)
print(results)

Simple way to sort a numerical list

numlists = ["5","50","7","51","87","97","53"]
results = list(map(int, numlists))
results.sort(reverse=False)
print(results)

回答 11

真正的问题是按字母数字排序。因此,如果您有一个列表[‘1’,’2’,’10’,’19’]并进行排序,则您会得到[‘1’,’10’。’19’,’2’]。即10排在2之前,因为它着眼于第一个字符并以此排序。似乎python中的大多数方法都按此顺序返回事物。例如,如果您有一个名为abc的目录,且文件标记为1.jpg,2.jpg等,最多说15.jpg,并且您执行file_list = os.listdir(abc),则file_list的顺序不是您期望的那样,而是file_list = [‘1.jpg’,’11 .jpg’—’15.jpg’,’2.jpg]。如果处理文件的顺序很重要(大概是您用数字命名它们的原因),那么该顺序就不是您认为的那样。您可以通过使用“零”填充来避免这种情况。例如,如果您有一个列表alist = [’01’,’03’,’05’,’10’,’02’,’04’,’06],然后对其进行排序,则会得到所需的顺序。alist = [’01’,’02’等],因为第一个字符是1之前的0。您需要填充的零个数由列表中的最大值确定。例如,如果最大的值介于100和1000,您需要填充001、002 — 010,011–100、101等个位数。

real problem is that sort sorts things alphanumerically. So if you have a list [‘1’, ‘2’, ’10’, ’19’] and run sort you get [‘1′, ’10’. ’19’, ‘2’]. ie 10 comes before 2 because it looks at the first character and sorts starting from that. It seems most methods in python return things in that order. For example if you have a directory named abc with the files labelled as 1.jpg, 2.jpg etc say up to 15.jpg and you do file_list=os.listdir(abc) the file_list is not ordered as you expect but rather as file_list=[‘1.jpg’, ’11.jpg’—’15.jpg’, ‘2.jpg]. If the order in which files are processed is important (presumably that’s why you named them numerically) the order is not what you think it will be. You can avoid this by using “zeros” padding. For example if you have a list alist=[’01’, ’03’, ’05’, ’10’, ’02’,’04’, ’06] and you run sort on it you get the order you wanted. alist=[’01’, ’02’ etc] because the first character is 0 which comes before 1. The amount of zeros padding you need is determined by the largest value in the list.For example if the largest is say between 100 and 1000 you need to pad single digits as 001, 002 —010,011–100, 101 etc.


回答 12

scores = ['91','89','87','86','85']
scores.sort()
print (scores)

这在python版本3中对我有用,尽管在版本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.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。