Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的字母数字排序和负数_Python_Sorting - Fatal编程技术网

Python中的字母数字排序和负数

Python中的字母数字排序和负数,python,sorting,Python,Sorting,我有一个相当简单的列表(一个数字后接一个句子),按正确的顺序排列: -347 a negative number -100 another negative number -25 and again, a negative number 17 some text 25 foo bar 100 two same texts 100 two same texts (almost) 350 a positive number 每次添加新项目时,我都需要对该列表进行排序 我搜索了S.O.并找到了答案:

我有一个相当简单的列表(一个数字后接一个句子),按正确的顺序排列:

-347 a negative number
-100 another negative number
-25 and again, a negative number
17 some text
25 foo bar
100 two same texts
100 two same texts (almost)
350 a positive number
每次添加新项目时,我都需要对该列表进行排序

我搜索了S.O.并找到了答案:

我使用的代码是freakish的,它是这样的:

import re
def convert(str):
    return int("".join(re.findall("\d*", str)))

list1.sort(key=convert)
为了更好地解释我的问题,我洗牌了列表并运行了代码

结果是:

17 some text
-25 and again, a negative number
25 foo bar
100 two same texts (almost)
100 two same texts
-100 another negative number
-347 a negative number
350 a positive number

出了什么问题?自然排序负数的代码精度是多少?

排序可以接受元组,因此首先按数字排序,然后按后面的文本排序

list_text = """-347 a negative number
-100 another negative number
-25 and again, a negative number
17 some text
25 foo bar
100 two same texts
100 two same texts (almost)
350 a positive number"""

list1 = list_text.split("\n")

list1.sort(key=lambda x: (int(x.split()[0]), x.split(" ",1)))

print("\n".join(list1))

IMHO最简单的方法是将字符串拆分为单个单词,然后按照转换为
int
的第一个单词进行排序:

list1.sort(key = lambda x : int(x.split(" ")[0]))
编辑:
与Keatinge一样,上述解决方案无法正确处理以相同数字开头的两个字符串。使用整个字符串作为辅助搜索词可以解决此问题:

list1.sort(key = lambda x : (int(x.split(" ")[0]), x)

'-'
不是数字,因此它与
'\d'
不匹配。因此,模式匹配只能找到完整的数字。旁注:如果您的输入字符串是,例如,
'1,这是第00001行。
它将按您的键排在最后。和
'
匹配
'\d*'
,但是
int('''
引发
ValueError
。您可以在运行之前将负数和正数分开,然后将负数粘在一起,使负数颠倒并位于顶部。您可以将转换函数更改为
返回int(str.split()[0])
这不会处理以相同数字开头但包含不同文本的两个字符串afterward@Keatinge说得好!使用字符串本身作为辅助搜索词应该可以做到这一点-请参阅我编辑的答案。为了避免第一次连接,可以将1作为参数传递给split-它将限制splits@Keatinge首先,谢谢,;但是我在[list1=list\u text.split(“\n”)]行上得到一个错误:AttributeError:'list'对象没有属性'split'。我不明白为什么(我的Python技能不值得一提)@ThG只使用最后两行,其他的是一个可运行的示例,所以每个人都可以运行它并进行测试it@Keatinge:它有效。现在我很尴尬:我有两个很好的答案,我已经“勾选”了Murelnik的答案。我不知道如何向S.O.表达我的敬意。无论如何,非常感谢。