Group Python List Elements

后端 未结 4 1875
误落风尘
误落风尘 2021-01-29 10:49

I have a python list as follows:

my_list = 

 [[25, 1, 0.65],
 [25, 3, 0.63],
 [25, 2, 0.62],
 [50, 3, 0.65],
 [50, 2, 0.63], 
 [50, 1, 0.62]]

4条回答
  •  走了就别回头了
    2021-01-29 11:17

    Use the following:

    my_list = [[25, 1, 0.65], [25, 3, 0.63], [25, 2, 0.62], [50, 3, 0.65], [50, 2, 0.63], [50, 1, 0.62]]
    
    list_25 = sorted([item for item in my_list if item[0] == 25], key=lambda item: item[1])
    list_50 = sorted([item for item in my_list if item[0] == 50], key=lambda item: item[1])
    
    res = [[i[2], j[2]] for i,j in zip(list_25, list_50)]
    

    Output:

    >>> res
    [[0.65, 0.62], [0.62, 0.63], [0.63, 0.65]]
    

提交回复
热议问题