简体   繁体   English

根据列表Python中的特定部分对列表进行排序

[英]Sort a list according a specific portion in the list Python

I have a list in the format: 我有一个格式的列表:

Apple, Orange[123 431]43351
Banana, Cherry[141 421]23423
Coconut, Mango[23 12312]232342
....
....

I want to sort the list according to the number after the bracket ']'. 我想根据方括号']'后的数字对列表进行排序。 The output should be: 输出应为:

Banana, Cherry[141 421]23423 
Apple, Orange[123 431]43351 
Coconut, Mango[23 12312]232342

I am trying to sort the list by using this: 我正在尝试使用此列表进行排序:

LIST.sort(key = lambda x: x.split()[1])
for item in LIST:
    print(item)

I can find the last number by this: But I am not able to sort it 我可以这样找到最后一个数字:但是我无法对其进行排序

for item in LIST:
    bracket_index = item.find("]")
    end_of_line = item[bracket_index + 1:]
    if bracket_index != -1:
        print(end_of_line)

What is the format of your list? 您的清单格式是什么? Is it a list of tuples or a list of strings? 它是元组列表还是字符串列表? This works: 这有效:

a = ['Apple, Orange[123 431]43351',
'Banana, Cherry[141 421]23423',
'Coconut, Mango[23 12312]232342']

a.sort(key = lambda el: el.split(']')[1])
print(a)

Output:
 ['Coconut, Mango[23 12312]232342',
 'Banana, Cherry[141 421]23423',
 'Apple, Orange[123 431]43351']

If it is a list of pairs of strings instead, then you should use key = lambda el: el[1].split(']')[1] like so: 如果它是一对字符串对的列表,则应使用key = lambda el: el[1].split(']')[1]如下所示:

a = [('Apple', 'Orange[123 431]43351'),
('Banana', 'Cherry[141 421]23423'),
('Coconut',' Mango[23 12312]232342')]

a.sort(key = lambda el: el[1].split(']')[1])
print(a)

Output:
[('Coconut', ' Mango[23 12312]232342'),
 ('Banana', 'Cherry[141 421]23423'),
 ('Apple', 'Orange[123 431]43351')]

You can go with @Psidom's suggestion: 您可以接受@Psidom的建议:

LIST.sort(key=lambda x: int(x.split(']')[1]))

Notice the use of int() to ensure the sort is done numerically rather than by string comparisons (which are done lazily; ie, '4321' is considered "larger" than '20000000' due to '4' > '2' ). 注意使用int()来确保排序是通过数字而不是通过字符串比较来完成的(这比较懒惰;即,由于'4' > '2''4321'被认为比'20000000'更大”)。


Full example: 完整示例:

LIST = [
    'Apple, Orange[123 431]43351',
    'Banana, Cherry[141 421]23423',
    'Coconut, Mango[23 12312]232342'
]

LIST.sort(key=lambda x: int(x.split(']')[1]))
print(LIST)

A better method would be to parse your strings first. 更好的方法是先解析您的字符串。 For example: 例如:

from collections import namedtuple
import re

FruitTuple = namedtuple('FruitTuple', ['fruit', 'color', 'num1', 'num2', 'num3'])

unparsed_list = [
    'Apple, Orange[123 431]43351',
    'Banana, Cherry[141 421]23423',
    'Coconut, Mango[23 12312]232342'
]

split_list = [re.search('(\\w+), (\\w+)\\[(\\d+) (\\d+)\\](\\d+)', x).groups() for x in unparsed_list]
fruit_list = [FruitTuple(*x) for x in split_list]
fruit_list.sort(key=lambda x: int(x.num2))

print(fruit_list)

Produces: 产生:

[FruitTuple(fruit='Banana', color='Cherry', num1='141', num2='421', num3='23423'), FruitTuple(fruit='Apple', color='Orange', num1='123', num2='431', num3='43351'), FruitTuple(fruit='Coconut', color='Mango', num1='23', num2='12312', num3='232342')] [FruitTuple(fruit ='Banana',color ='Cherry',num1 ='141',num2 ='421',num3 ='23423'),FruitTuple(fruit ='Apple',color ='Orange',num1 = '123',num2 ='431',num3 ='43351'),FruitTuple(fruit ='Coconut',color ='Mango',num1 = '23',num2 ='12312',num3 ='232342')]]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM