How to separate a Python list into two lists, according to some aspect of the elements

后端 未结 8 820
小鲜肉
小鲜肉 2021-01-06 05:23

I have a list like this:

[[8, \"Plot\", \"Sunday\"], [1, \"unPlot\", \"Monday\"], [12, \"Plot\", \"Monday\"], [10, \"Plot\", \"Tuesday\"], [4, \"unPlot\", \"         


        
8条回答
  •  梦谈多话
    2021-01-06 05:56

    Use list comprehension:

    l = [[8, "Plot", "Sunday"], [1, "unPlot", "Monday"], [12, "Plot", "Monday"], [10, "Plot", "Tuesday"], [4, "unPlot", "Tuesday"], [14, "Plot", "Wednesday"], [6, "unPlot", "Wednesday"], [1, "unPlot", "Thursday"], [19, "Plot", "Thursday"], [28, "Plot", "Friday"], [10, "unPlot", "Friday"], [3, "unPlot", "Saturday"]]
    
    list1 = [x for x in l if x[1] == "Plot"]
    
    list2 = [x for x in l if x[1] == "unPlot"]
    

提交回复
热议问题