我有例子的目录列表:名单列表的字符串
sample = [['a'],['b']]
我想这个名单被转换为字符串列表像
sample = ['a','b']
我以前使用的列表,但这似乎在打扰我! 在此先感谢
我有例子的目录列表:名单列表的字符串
sample = [['a'],['b']]
我想这个名单被转换为字符串列表像
sample = ['a','b']
我以前使用的列表,但这似乎在打扰我! 在此先感谢
试试这个:
In [5]: sample = [['a'],['b']]
In [6]: [item for sublist in sample for item in sublist]
Out[6]: ['a', 'b']
或
In [10]: from itertools import chain
In [11]: list(chain.from_iterable(sample))
Out[11]: ['a', 'b']
您还没有覆盖所有的情况,但是:
[''.join(x) for x in sample]
简单的循环就足够了 -
>>> s = [['a'],['b']]
>>> t = []
>>> for a in s:
for b in a:
t.append(b)
>>> t
['a', 'b']
hrmm ...'in for for'? –
将'a'改名为'sample',更清楚? –
@FredrikPihl谢谢你! – Sangamesh