简体   繁体   English

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

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

I have a list like this: 我有一个这样的列表:

[[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"]]

I want to separate it into two lists according the Plot and unPlot values, resulting: 我想根据PlotunPlot值将它分成两个列表,结果如下:

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

Try with basic list comprehension: 尝试基本列表理解:

>>> [ 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']]

Or with filter if you fancy functional programming: 如果您喜欢函数式编程,请使用filter

>>> 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']]

I personally find list comprehensions much clearer. 我个人觉得列表理解更加清晰。 It's certainly the most "pythonic" way. 它肯定是最“pythonic”的方式。

data = [[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"]]

res = {'Plot':[],'unPlot':[]}
for i in data: res[i[1]].append(i)

This way you iterate the list once 这样您可以迭代列表一次

Try: 尝试:

yourList=[[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"]]
plotList=[]
unPlotList=[]

for i in yourList:
    if "Plot" in i:
        plotList.append(i)
    else:
        unPlotList.append(i)

or shorter with comprehension: 理解时更短或更短:

plotList = [i for i in yourList if "Plot" in i]
unPlotList = [i for i in yourList if "unPlot" in i]

You could use list comprehensions, eg 您可以使用列表推导,例如

# 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']

If you want to traverse the input list only once, then maybe: 如果只想遍历输入列表一次,那么可能:

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

You could simply go through the list, and check if the value is "Plot" like this: 您可以直接浏览列表,并检查值是否为“Plot”,如下所示:

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

I have a helper function for the general case of partitioning a list in two: 我有一个辅助函数,用于将列表分成两部分的一般情况:

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, ([], []))

For example: 例如:

>>> partition([1, 2, 3, 4], lambda d: d % 2 == 0)
([2, 4], [1, 3])

Or in your case: 或者在你的情况下:

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

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"]

You can also do it with the filter command: 您也可以使用filter命令执行此操作:

list1 = filter(lambda x: x[1] == "Plot", list)
list2 = filter(lambda x: x[1] == "unPlot", list)

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

相关问题 如何根据条件将一个 python 列表分成 3 个不同的列表 - How to separate one python list into 3 different lists according to the criteria 如何将python列表上的每月和每天的文件名分成两个单独的列表? - How to separate monthly and daily filenames on python list into two separate lists? 如何根据每个子列表第一个条目中的值将 python 列表列表拆分为 3 个单独的列表? - How to split a python list of lists into 3 separate lists according to their value in the each sublist's first entry? 根据位置将嵌套列表中的元素提取到单独的列表中 - Extract elements in a nested list into separate lists according to the positions Python-在单独的行的两个列表中的输出元素 - Python - Output Elements in Two Lists on Separate Lines 根据python中两个单独列表的索引组合两个列表的元素 - Combining elements of two lists based on the indexes of two separate lists in python 如何将一个列表分为两个不同的列表?(Python2.7) - How to separate a list into two different lists ?(Python2.7) Python-如何将列表动态拆分为两个单独的列表 - Python - How to split a list into two separate lists dynamically 交换 Python 列表列表中的两个元素 - Swapping two elements in a list of lists of lists in Python 如何将 .csv 的元素拆分为两个单独的列表 - How to split elements of .csv into two separate lists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM