How to sort nested lists into seperate lists with unique values in python?

后端 未结 2 795
隐瞒了意图╮
隐瞒了意图╮ 2021-01-23 14:05

I have two variables:

unique_val = [1,2,3]
nested_list = [[\'name1\',1],[\'name2\',1],[\'name3\',3],[\'name4\',2],[\'name5\',2],[\'name6\',3]]

2条回答
  •  走了就别回头了
    2021-01-23 14:48

    you can use list comprehension:

    [[name for name, number in nested_list if number == n] for n in unique_val]
    

    if you really want to put it in separate variable, you can do the following.

    list_1, list_2, list_3 = [[name for name, number in nested_list if number == n] for n in unique_val]
    

提交回复
热议问题