简体   繁体   English

如何按数字顺序而不是字母顺序对列表中的元素进行排序

[英]how to sort the elements in a list in numerical order and not alphabetical

i have a list containing sentences with numbers in it, i want to sort then numerically and not alphabetically.我有一个列表,其中包含带有数字的句子,我想按数字而不是字母顺序排序。 but when i use the sort function, it sorts the elements alphabetically and not numerically, how do i fix this?但是当我使用排序 function 时,它按字母而不是数字对元素进行排序,我该如何解决这个问题?

num = ['ryan has 8 apples','charles has 16 bananas','mylah has 3 watermelons']
num.sort()
print(num)

output:
['charles.....','mylah.....','ryan......']

as you can see, it is sorted alphabetically but that is not my expected output如您所见,它按字母顺序排序,但这不是我预期的 output

the dots represent the rest of the sentence点代表句子的 rest

expected result:

['mylah has 3 watermelons','ryan has 8 apples','charles has 16 bananas']

here's the expected output where the elements are sorted numerically and not alphabetically这是预期的 output ,其中元素按数字而非字母顺序排序

How about put the list in the form of dict, with the key as the numeric value extracted from the string, and the value as the string?将列表以dict的形式放入,key为从字符串中提取的数值,value为字符串?

Then, you could sort the dict in terms of the key?然后,您可以根据键对字典进行排序吗?

You need to pass in a key to sort them on that splits the string and parses the number as a number to sort, you can also then sort alphabetically if you wish for those with the same number.您需要传入一个键来对它们进行排序,该键将字符串拆分并将数字解析为要排序的数字,如果您希望具有相同数字的那些,您也可以按字母顺序排序。

sorted(num, key=lambda x: (int(x.split()[2]), x))

or或者

num.sort(key=lambda x: (int(x.split()[2]), x))

You need to use key= to let sort know which value you want to assign each element for sorting.您需要使用key=sort知道您要为每个元素分配哪个值进行排序。

If the strings are always that structured, you can use:如果字符串始终是结构化的,您可以使用:

num = ['ryan has 8 apples','charles has 16 bananas','mylah has 3 watermelons']
num.sort(key=lambda x: int(x.split(' ')[2]))
print(num)

If they are more complex, take a look at regular expressions.如果它们更复杂,请查看正则表达式。

The solutions so far focus on a solution specific to the structure of your strings but would fail for slightly different string structures.到目前为止,解决方案专注于特定于字符串结构的解决方案,但会因字符串结构略有不同而失败。 If we extract only the digit parts of the string and then convert to an int , we'll get a value that we can pass to the sort function's key parameter:如果我们只提取字符串的数字部分,然后转换为int ,我们将得到一个可以传递给排序函数的 key 参数的值:

num.sort(
    key=lambda s: int(''.join(c for c in s if c.isdigit()))
)

The key parameter lets you specify some alternative aspect of your datum to sort by, in this case, a value provided by the lambda function. key 参数允许您指定要排序的数据的某些替代方面,在这种情况下,由 lambda function 提供的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不能使用sorted函数对两个元素元组列表按降序排列,然后在出现领带的情况下按字母升序排序 - I can't use the sorted function to sort a two elements tuple list in descending numerical order then in ascending alphabetical order in case of a tie 如何按字母+数字顺序按列对表格可视化进行排序? - How to sort table visualization by column in alphabetical + numerical order? 如何在python中按字母顺序对列表进行排序? - how to sort list in alphabetical order in python? "如何按长度对列表进行排序,然后按字母倒序排列" - How to sort a list by length and then in reverse alphabetical order 根据长度第一和字母顺序第二对列表中的元素进行排序 - Sort elements in list based on length first and alphabetical order second 以降序对列表进行排序,如果数字相同,则在 Python 中按字母顺序升序排序 - Sorting a list in Descending numerical order and if number is the same, sort by Ascending alphabetical in Python 首先根据长度对字符串元素列表进行排序,然后根据长度匹配的元素的字母顺序对其进行排序 - Order a list of string elements based on length first and then sort it based on alphabetical order for elements with matching length 如何使用python以给定的字母顺序排序列表? - How to a sort list in the given alphabetical order using python? 如何编写正确的代码:在函数内按字母顺序对列表进行排序? - How to write the correct code: sort a list in alphabetical order within a function? 如何按字母顺序对字典进行排序? - How to sort dictionary in alphabetical order?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM