我有一个这样的清单:
x = [[(u'reads_2.fq',), (u'reads_2.fq',), (u'reads_2.fq',), (u'reads_2.fq',), (u'reads_2.fq',)]]
我想将此列表转换为:
x = ['reads_2.fq', 'reads_2.fq', 'reads_2.fq', 'reads_2.fq', 'reads_2.fq']
答案 0 :(得分:2)
使用列表理解:
>>> x = [[(u'reads_2.fq',), (u'reads_2.fq',), (u'reads_2.fq',), (u'reads_2.fq',), (u'reads_2.fq',)]]
>>> [y[0] for y in x[0]]
[u'reads_2.fq', u'reads_2.fq', u'reads_2.fq', u'reads_2.fq', u'reads_2.fq']
如果内部元组包含多个项目,请使用itertools.chain.from_iterable
:
>>> from itertools import chain
>>> list(chain.from_iterable(x[0]))
[u'reads_2.fq', u'reads_2.fq', u'reads_2.fq', u'reads_2.fq', u'reads_2.fq']