简体   繁体   English

按行对列表列表进行垂直排序

[英]sort a list of lists by row, vertically

very new to coding, want to compare the same indices within the list of lists in order to align the numbers vertically so they are in numerical order:对编码非常陌生,想要比较列表列表中的相同索引,以便垂直对齐数字,使它们按数字顺序排列:

Write a function called sort_by_direction that takes two parameters.编写一个名为 sort_by_direction 的 function,它接受两个参数。 The first parameter called seq will be a list of lists where the number of lists is equal to the number of items in each list.第一个称为 seq 的参数将是列表的列表,其中列表的数量等于每个列表中的项目数。 The second parameter called direc will give the direction to sort each list of lists.第二个名为 direc 的参数将给出对每个列表进行排序的方向。 direc will have four possible values, "L" which will sort each row in ascending order, "R" which will sort each row in descending order, "U" which sorts each column in ascending order, and "D" which sorts each column in descending order. direc 将有四个可能的值,“L”将按升序对每一行进行排序,“R”将按降序对每一行进行排序,“U”对每一列按升序排序,“D”对每一列排序按降序排列。

example = [[3, 6, 1], [5, 2, 2], [0, 8, 7]]
sort_by_direction(example, 'L') # should return [[1, 3, 6], [2, 2, 5], [0, 7, 8]]

example = [[3, 6, 1], [5, 2, 2], [0, 8, 7]]
sort_by_direction(example, 'R') # should return [[6, 3, 1], [5, 2, 2], [8, 7, 0]]

example = [[3, 6, 1], [5, 2, 2], [0, 8, 7]]
sort_by_direction(example, 'U') # should return [[5, 8, 7], [3, 6, 2], [0, 2, 1]]

[[5, 8, 7],

 [3, 6, 2],

 [0, 2, 1]]

example = [[3, 6, 1], [5, 2, 2], [0, 8, 7]]
sort_by_direction(example, 'D') # should return [[0, 2, 1], [3, 6, 2], [5, 8, 7]]

[[0, 2, 1],

 [3, 6, 2],

 [5, 8, 7]]

def sort_by_direction(seq,direc):
    new = []
    if direc == 'L':
        for lst in seq:
            lst.sort()
            new.append(lst)
    elif direc == 'R':
        for lst in seq:
            lst.sort()
            lst.reverse()
            new.append(lst)
    elif direc == 'U':
        for lst in seq:
            for idx, num in enumerate(zip(lst)):
            ????

Sorting every row is simple.对每一行进行排序很简单。

Ascending:上升:

[sorted(x) for x in example]

Descending:降序:

[sorted(x, reverse = True) for x in example]

To sort column-wise, you could transpose the nested list first, then sort the same way as shown above, then transpose back.要按列排序,您可以先转置嵌套列表,然后按照与上所示相同的方式排序,然后转回。 An easy way to transpose a list of lists:转置列表列表的简单方法:

transposed = list(zip(*example))

Then, sort it and transpose back:然后,对其进行排序并转回:

tmp = [sorted(x, reverse = True) for x in transposed]
result = list(zip(*tmp))

And of course it was already in "The Simpsons" NumPy:当然,它已经出现在“辛普森一家” NumPy 中:

import numpy
example = numpy.array([[3, 6, 1], [5, 2, 2], [0, 8, 7]])
result = numpy.sort(example, axis = 0) # only use axis = 0 to sort vertically
# add this to reverse the order:
result = numpy.flip(result, axis = 0) # only use axis = 0 if sorted vertically

You can use pandas for that:您可以为此使用 pandas :

def sort_by_direction(seq,direc):
    new = []
    if direc == 'L':
        for lst in seq:
            lst.sort()
            new.append(lst)
    elif direc == 'R':
        for lst in seq:
            lst.sort()
            lst.reverse()
            new.append(lst)
    elif direc == 'U':
         df = pd.DataFrame(data=seq)
         k = df.apply(lambda x: (x.sort_values(ascending=False)).to_list(),axis=0)
         return k.values
    
    elif direc == 'D':
         df = pd.DataFrame(data=seq)
         k = df.apply(lambda x: (x.sort_values(ascending=True)).to_list(),axis=0)
         return k.values
        
example = [[3, 6, 1], [5, 2, 2], [0, 8, 7]]
sort_by_direction(example, 'D') 

Result:结果:

array([[0, 2, 1],
       [3, 6, 2],
       [5, 8, 7]])

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

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