如何在Python的以下列表中转换此列表/元组?

时间:2013-07-02 06:07:19

标签: python list

我有一个这样的清单:

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']

1 个答案:

答案 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']