Python:展平内部列表时保留外部列表

时间:2019-03-20 23:40:58

标签: python list nested list-comprehension flatten

我有一个看起来像这样的列表列表。我想删除内部列表,并保留外部列表。我不想将整个内容简化为庞大的元素列表。

['100', ['ADDRESS', 'START', '100']]
['. TESTS PC FORWARD ADDRESSING']
['. TESTS BASE ADDRESSING']
['. TESTS PC BACKWARD ADDRESSING']
['100', ['NOW', '+LDB', '#BEGIN', 'load base register']]
['104', ['XXX', 'BASE', 'BEGIN', 'tell assembler']]
['104', ['YYY', 'LDA', 'BE', 'A <- (m..m+2)']]
['107', ['EN', 'RESW', '4']]
['113', ['BE', 'WORD', '2']]
['116', ['BEGIN', 'RESW', '1']]
['119', ['JUNK', 'RESW', '2200']]
['1ae1', ['THERE', 'RESW', '1']]
['1ae4', ['ZZZ', 'LDA', 'JUNK']]
['1ae7', ['MMM', 'STA', 'THERE']]
['1aea', ['', 'END', 'NOW']]

我想要的是这个

['100', 'ADDRESS', 'START', '100']
['. TESTS PC FORWARD ADDRESSING']
['. TESTS BASE ADDRESSING']
['. TESTS PC BACKWARD ADDRESSING']
['100', 'NOW', '+LDB', '#BEGIN', 'load base register']
['104', 'XXX', 'BASE', 'BEGIN', 'tell assembler']
['104', 'YYY', 'LDA', 'BE', 'A <- (m..m+2)']
['107', 'EN', 'RESW', '4']
['113', 'BE', 'WORD', '2']
['116', 'BEGIN', 'RESW', '1']
['119', 'JUNK', 'RESW', '2200']
['1ae1', 'THERE', 'RESW', '1']
['1ae4', 'ZZZ', 'LDA', 'JUNK']
['1ae7', 'MMM', 'STA', 'THERE']
['1aea', '', 'END', 'NOW']

我的代码: newlist = [str(sublist[0]) + str(sublist[1:]) for sublist in final_file]

这段代码给了我这个输出,而不是我想要的:

100[['ADDRESS', 'START', '100']]
. TESTS PC FORWARD ADDRESSING[]
. TESTS BASE ADDRESSING[]
. TESTS PC BACKWARD ADDRESSING[]
100[['NOW', '+LDB', '#BEGIN', 'load base register']]
104[['XXX', 'BASE', 'BEGIN', 'tell assembler']]
104[['YYY', 'LDA', 'BE', 'A <- (m..m+2)']]
107[['EN', 'RESW', '4']]
113[['BE', 'WORD', '2']]
116[['BEGIN', 'RESW', '1']]
119[['JUNK', 'RESW', '2200']]
1ae1[['THERE', 'RESW', '1']]
1ae4[['ZZZ', 'LDA', 'JUNK']]
1ae7[['MMM', 'STA', 'THERE']]
1aea[['', 'END', 'NOW']]

4 个答案:

答案 0 :(得分:2)

这是一种简单的 for-loop 方法,可为您提供通用方法。

def unpacker(lists):
    for list_ in lists:
        o = []
        for item in list_:
            if isinstance(item, list):
                for i in item:
                    o.append(i)
            else:
                o.append(item)
        yield o

然后您将其命名为-> list(unpacker(l))

答案 1 :(得分:0)

更新:

newlist = [[x[0]] + x[1] if len(x) >= 2 else x for x in final_file]

这有效

答案 2 :(得分:0)

newlist = ['104', ['YYY', 'LDA', 'BE', 'A <- (m..m+2)']]


final = []
for n in newlist:
    if type(n) == list:
        for i in n:
            final.append(i)
    else:
        final.append(n)

print(final)

您可以检查元素的类型是否为列表,如果是,则使用另一个循环将其解压缩并将其附加到新列表中。

答案 3 :(得分:0)

如果有问题的列表是

a = [
['100', ['ADDRESS', 'START', '100']],
['. TESTS PC FORWARD ADDRESSING'],
['. TESTS BASE ADDRESSING'],
['. TESTS PC BACKWARD ADDRESSING'],
['100', ['NOW', '+LDB', '#BEGIN', 'load base register']],
['104', ['XXX', 'BASE', 'BEGIN', 'tell assembler']],
['104', ['YYY', 'LDA', 'BE', 'A <- (m..m+2)']],
['107', ['EN', 'RESW', '4']],
['113', ['BE', 'WORD', '2']],
['116', ['BEGIN', 'RESW', '1']],
['119', ['JUNK', 'RESW', '2200']],
['1ae1', ['THERE', 'RESW', '1']],
['1ae4', ['ZZZ', 'LDA', 'JUNK']],
['1ae7', ['MMM', 'STA', 'THERE']],
['1aea', ['', 'END', 'NOW']],
]

单线:

[sum([sub if isinstance(sub, list) else [sub] for sub in x], []) for x in a]

如果只需要单个列表,则删除外部循环;

sum([sub if isinstance(sub, list) else [sub] for sub in a[0]], [])