从元组列表

时间:2018-05-25 20:56:10

标签: python list tuples

我有一个这样的清单:

mylist = [(20, 'Start', '2008-10-10', 'TBS'),...,(20, 'End', '2008-11-09', 'NG'), 
          (21, 'Start', '2008-12-10', 'TBS'),...,(21, 'End', '2008-12-15', 'G'), 
          (22, 'Start', '2009-01-10', 'TBS'),...,(22, 'End', '2009-12-10', 'B'),..]

我在上面的示例中添加了'...',表示每个ID都有其他项目,例如列表中的20,21和22,但我不想要它们。我想要的唯一项目是包含“开始”或“结束”的项目。(其他项目的单词与这两个单词不同。)

我想创建一个这样的嵌套列表:

[[20, 'Start', '2008-10-10', 'End', '2008-11-09', 'NG'] ,
 [21, 'Start', '2008-12-10', 'End', '2008-12-15', 'G'], 
 [22, 'Start', '2009-01-10', 'End', '2009-12-10', 'B']]

这是我的代码:

code = 0
brr = []
for row in myList:
    if row[1] == "Start":
        arr = []
        code = row[0]
        arr.append([row[0], row[1], row[2]])
        continue

    if row[0] == code and row[1] == "End":
        arr.append([row[1], row[2], row[3]])
    brr.append(arr)
for k in brr:
    print(k)

但问题在于它创造了这样的东西:

[[[20, 'Start', '2008-10-10', 'End'], ['2008-11-09', 'NG']] ,
 [[20, 'Start', '2008-10-10', 'End'], ['2008-11-09', 'NG']] ,
 [[20, 'Start', '2008-10-10', 'End'], ['2008-11-09', 'NG']] ,
 [[21, 'Start', '2008-12-10', 'End'], ['2008-12-15', 'G']], 
 [[21, 'Start', '2008-12-10', 'End'], ['2008-12-15', 'G']],
 [[22, 'Start', '2009-01-10', 'End'], ['2009-12-10', 'B']]]

对于每个项目,我在列表中有多行。我不知道为什么? 对不起,如果我的问题太长了。

4 个答案:

答案 0 :(得分:1)

您需要使用arr.extend()函数

arr = []
arr.append([1,2]) # arr = [[1,2]]
arr = []
arr.extend([1,2])  # arr = [1,2]

答案 1 :(得分:1)

您只需使用itertools.groupby即可实现此目的:

import itertools
from pprint import pprint

mylist = [
    (20, 'Start', '2008-10-10', 'TBS'),
    (20, 'Foo', '2008-10-10', 'TBS'),
    (20, 'End', '2008-11-09', 'NG'),

    (21, 'Start', '2008-12-10', 'TBS'),
    (21, 'End', '2008-12-15', 'G'),

    (22, 'Start', '2009-01-10', 'TBS'),
    (22, 'End', '2009-12-10', 'B'),
]

rows = (x for x in mylist if x[1] in ('Start', 'End'))
grouped = itertools.groupby(rows, key=lambda x: x[0])
output = [[k, *next(grp)[1:3], *next(grp)[1:4]] for k, grp in grouped]
pprint(output)

输出:

[[20, 'Start', '2008-10-10', 'End', '2008-11-09', 'NG'],
 [21, 'Start', '2008-12-10', 'End', '2008-12-15', 'G'],
 [22, 'Start', '2009-01-10', 'End', '2009-12-10', 'B']]

答案 2 :(得分:0)

您的brr.append(arr)始终为每一行添加一个数组,这就是为什么输出中有6个元素的原因。将brr.append(arr)更改为:

if arr not in brr:
    brr.append(arr)

对于格式,arr.append([row[0], row[1], row[2]])添加了3个元素的列表,而不是3个单独的元素。请改用extend

您的最终代码应如下所示:

code = 0
brr = []
for row in mylist:

    if row[1] == "Start":
        arr = []
        code = row[0]
        arr.extend([row[0], row[1], row[2]])
        # continue not needed here

    if row[0] == code and row[1] == "End":
        arr.extend([row[1], row[2], row[3]])

    if arr not in brr:
        brr.append(arr)

for k in brr:
    print(k)

答案 3 :(得分:0)

请试试这个,

startlist=[]
endlist=[]
for item in mylist:
    if 'Start' in list(item):
        startlist.append(list(item))
    elif 'End' in list(item):
        endlist.append(list(item))
outlist=[i+j for i,j in zip(startlist,endlist)]