如何根据元素的某些方面将Python列表分为两个列表

如何根据元素的某些方面将Python列表分为两个列表,python,Python,我有这样一份清单: [[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, "Pl

我有这样一份清单:

[[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"]]
我想根据
Plot
unPlot
值将其分为两个列表,结果是:

list1=[[8, "Plot", "Sunday"], [12, "Plot", "Monday"], ...]
list2=[[1, "unPlot", "Monday"], [4, "unPlot", "Tuesday"], ...]
尝试:

或更简短的理解:

plotList = [i for i in yourList if "Plot" in i]
unPlotList = [i for i in yourList if "unPlot" in i]
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"]
>>> [ x for x in l if x[1] == "Plot" ]
[[8, 'Plot', 'Sunday'], [12, 'Plot', 'Monday'], [10, 'Plot', 'Tuesday'], [14, 'Plot', 'Wednesday'], [19, 'Plot', 'Thursday'], [28, 'Plot', 'Friday']]
>>> [ x for x in l if x[1] == "unPlot" ]
[[1, 'unPlot', 'Monday'], [4, 'unPlot', 'Tuesday'], [6, 'unPlot', 'Wednesday'], [1, 'unPlot', 'Thursday'], [10, 'unPlot', 'Friday'], [3, 'unPlot', 'Saturday']]

使用列表理解:

plotList = [i for i in yourList if "Plot" in i]
unPlotList = [i for i in yourList if "unPlot" in i]
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"]
>>> [ x for x in l if x[1] == "Plot" ]
[[8, 'Plot', 'Sunday'], [12, 'Plot', 'Monday'], [10, 'Plot', 'Tuesday'], [14, 'Plot', 'Wednesday'], [19, 'Plot', 'Thursday'], [28, 'Plot', 'Friday']]
>>> [ x for x in l if x[1] == "unPlot" ]
[[1, 'unPlot', 'Monday'], [4, 'unPlot', 'Tuesday'], [6, 'unPlot', 'Wednesday'], [1, 'unPlot', 'Thursday'], [10, 'unPlot', 'Friday'], [3, 'unPlot', 'Saturday']]

尝试基本列表理解:

plotList = [i for i in yourList if "Plot" in i]
unPlotList = [i for i in yourList if "unPlot" in i]
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"]
>>> [ x for x in l if x[1] == "Plot" ]
[[8, 'Plot', 'Sunday'], [12, 'Plot', 'Monday'], [10, 'Plot', 'Tuesday'], [14, 'Plot', 'Wednesday'], [19, 'Plot', 'Thursday'], [28, 'Plot', 'Friday']]
>>> [ x for x in l if x[1] == "unPlot" ]
[[1, 'unPlot', 'Monday'], [4, 'unPlot', 'Tuesday'], [6, 'unPlot', 'Wednesday'], [1, 'unPlot', 'Thursday'], [10, 'unPlot', 'Friday'], [3, 'unPlot', 'Saturday']]
如果您喜欢函数式编程,也可以使用
过滤器

>>> filter(lambda x: x[1] == "Plot", l)
[[8, 'Plot', 'Sunday'], [12, 'Plot', 'Monday'], [10, 'Plot', 'Tuesday'], [14, 'Plot', 'Wednesday'], [19, 'Plot', 'Thursday'], [28, 'Plot', 'Friday']]
>>> filter(lambda x: x[1] == "unPlot", l)
[[1, 'unPlot', 'Monday'], [4, 'unPlot', 'Tuesday'], [6, 'unPlot', 'Wednesday'], [1, 'unPlot', 'Thursday'], [10, 'unPlot', 'Friday'], [3, 'unPlot', 'Saturday']]

我个人认为清单的理解要清楚得多。这当然是最“python”的方式。

你可以使用列表理解,例如

# old_list elements should be tuples if they're fixed-size, BTW
list1 = [(X, Y, Z) for X, Y, Z in old_list if Y == 'Plot']
list2 = [(X, Y, Z) for X, Y, Z in old_list if Y == 'unPlot']
如果只想遍历输入列表一次,则可能:

def split_list(old_list):
    list1 = []
    list2 = []
    for X, Y, Z in old_list:
        if Y == 'Plot':
            list1.append((X, Y, Z))
        else:
            list2.append((X, Y, Z))
    return list1, list2

您只需浏览列表,并检查值是否为如下所示的“绘图”:

for i in List:
  if i[1]=="Plot":
    list1.append(i)
  else:
    list2.append(i)

也可以使用以下命令执行此操作:


对于将列表分为两部分的一般情况,我有一个帮助函数:

def partition(iterable, condition):
        def partition_element(partitions, element):
            (partitions[0] if condition(element) else partitions[1]).append(element)
            return partitions
        return reduce(partition_element, iterable, ([], []))
例如:

>>> partition([1, 2, 3, 4], lambda d: d % 2 == 0)
([2, 4], [1, 3])
或者在您的情况下:

>>> partition(your_list, lambda i: i[1] == "Plot")

通过这种方式,一旦ezdazuzena得到了列表,就可以对其进行迭代。。。那应该有用。。。只需确保首先将plotlist和unplotlist定义为空列表。感谢您提供如此简单的解决方案。