简体   繁体   English

对列表列表中的元素进行排序

[英]Sorting elements in a list of lists

Hey I have a list that goes like:嘿,我有一个列表,如下所示:

List = [[[1,2],[4,5,3]],[[5,2],[3,4,7]],...]

and I want to sort the elements of each sublist, that the output looks like:我想对每个子列表的元素进行排序,输出如下所示:

List = [[[2,1],[5,4,3]],[[5,2],[7,4,3]],...]

What I have already tried is creating a loop like this.我已经尝试过创建这样的循环。

for i in List:
    for u in [i]:
        List[u].sort(reverse = True)
        print(List)

but it didnt worked.但它没有用。 In other Blogposts I just found ways how to sort the lists, but not the elements in them.在其他博客文章中,我只是找到了如何对列表进行排序的方法,但没有找到对其中的元素进行排序的方法。 Thanks for your help谢谢你的帮助

You may use a list comprehension, to easily retrieve the values, iterate over each sublist and sort the subsublist您可以使用列表理解来轻松检索值,遍历每个子列表并对子子列表进行排序

values = [[[1, 2], [4, 5, 3]], [[5, 2], [3, 4, 7]]]
values = [[sorted(subsub, reverse=True) for subsub in sub] for sub in values]
print(values) # [[[2, 1], [5, 4, 3]], [[5, 2], [7, 4, 3]]]

Or with loops, you can just access the inner lists with the second loop and inline sort them或者使用循环,您可以使用第二个循环访问内部列表并对它们进行内联排序

for sub in values:
    for subsub in sub:
        subsub.sort(reverse=True)

Note: use meaningful names to your variables注意:为变量使用有意义的名称

You are close, just a few things wrong:你很接近,只是一些错误:

for u in [i]: Do not wrap i in brackets. for u in [i]:不要将i括在括号中。 You are just creating a new list with i as its only element, and looping over that which is not what you want.您只是在创建一个新列表,将i作为其唯一元素,并循环遍历您不想要的列表。

Second:第二:

List[u].sort(reverse = True) You do not need to do List[u] , rather just u , because inside of this loop, u is equal to the sublist that you want to sort List[u].sort(reverse = True)你不需要做List[u] ,而只是u ,因为在这个循环内部, u等于你想要排序的子列表

Also a side note, variables should be lowercase还有一个旁注,变量应该是小写的

myList = [[[1,2],[4,5,3]],[[5,2],[3,4,7]]]
for i in myList: 
    for u in i: 
        u.sort(reverse=True) 
print(myList)

You are close with your approach although in your second loop you're enclosing i in a list which won't loop through each sub list and you only need to sort u as you're not going to slice a list with a list.你很接近你的方法,尽管在你的第二个循环中你将i包含在一个列表中,该列表不会遍历每个子列表,你只需要对u进行排序,因为你不会用列表切片列表。

for i in List:
    for u in i:
        u.sort(reverse = True)

print(List)

Result:结果:

[[[2, 1], [5, 4, 3]], [[5, 2], [7, 4, 3]]]

PS If there could be only one layer of nesting then I'd suggest using isinstance to check before looping through and error-ing out due to attempting to sort an int PS如果可能只有一层嵌套,那么我建议在循环之前使用isinstance进行检查并由于尝试对int进行排序而出错

for i in List:
    if isinstance(i[0], list):
        for u in i:
            u.sort(reverse = True)
    else:
        i.sort(reverse = True)

Another approach using list comprehension (one liner):另一种使用list comprehension (一个班轮):

li = [[[1,2],[4,5,3]],[[5,2],[3,4,7]]]

li = [[sorted(i, reverse=True) for i in j] for j in li]

print(li)

Output:输出:

[[[2, 1], [5, 4, 3]], [[5, 2], [7, 4, 3]]]

Is this what you are looking for?这是你想要的?

for i in List:
    for u in i:
        u.sort(reverse = True)

You can use Pandas to sort your data.您可以使用 Pandas 对数据进行排序。

import pandas as pd

ist = [[[1,2],[4,5,3]],[[5,2],[3,4,7]]]
df = pd.DataFrame(ist)

I named the columns to read the columns easier.我为这些列命名是为了更容易阅读这些列。

df.columns = ['a', 'b']

Now we can sort the elements of the lists in each,现在我们可以对每个列表的元素进行排序,

df['a'] = df.a.apply(lambda x: sorted(x,reverse=True))
df['b'] = df.b.apply(lambda x: sorted(x,reverse=True))

which yields,这产生,

print(df.head())

    a          b
0  [2, 1]  [5, 4, 3]
1  [5, 2]  [7, 4, 3]

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

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