我有一个这样的清单:
x = [[(u'reads_2.fq',)], [], [(u'README.txt',)]]
期望的输出:
['reads.fq', 'README.txt']
答案 0 :(得分:0)
列表理解:
>>> [a for b in x for c in b for a in c]
[u'reads_2.fq', u'README.txt']
或者如果您使用的是Python 2.7(请注意,compiler
模块已弃用,并且在python 3中不可用):
>>> from compiler.ast import flatten
>>> flatten(x)
[u'reads_2.fq', u'README.txt']
答案 1 :(得分:0)
你可以试试这个:
>>> y = []
>>> for a in x:
... if a:
... y.append(a[0][0])
...
>>> y
[u'reads_2.fq', u'README.txt']