Python:展平内部列表时保留外部列表
Posted
技术标签:
【中文标题】Python:展平内部列表时保留外部列表【英文标题】:Python: Keep outer list when flattening inner list 【发布时间】:2019-08-11 19:01:59 【问题描述】:我有一个看起来像这样的列表。我想删除内部列表,并保留外部列表。我不想把整个东西扁平化成一个巨大的元素列表。
['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']]
【问题讨论】:
【参考方案1】:这是一个简单的 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))
【讨论】:
【参考方案2】:更新:
newlist = [[x[0]] + x[1] if len(x) >= 2 else x for x in final_file]
这行得通
【讨论】:
这假设它们以某种方式排序【参考方案3】:如果有问题的列表是;
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]], [])
【讨论】:
【参考方案4】: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)
您可以检查元素的类型是否为列表,如果是则使用另一个循环将其解包并将其附加到新列表中。
【讨论】:
以上是关于Python:展平内部列表时保留外部列表的主要内容,如果未能解决你的问题,请参考以下文章
如何在保留列表名称和值的同时展平一列列表? [r] [重复]