我有一个这样的列表:
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']
使用列表推导:
>>> 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']