我有一个列表列表例如:
sample = [['a'],['b']]
我希望将此列表转换为类似
的字符串列表sample = ['a','b']
之前我使用过列表,但这似乎困扰着我! 提前致谢
答案 0 :(得分:1)
试试这个:
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']
答案 1 :(得分:0)
您尚未涵盖所有案例,但是:
[''.join(x) for x in sample]
答案 2 :(得分:0)
简单的循环就足够了 -
>>> s = [['a'],['b']]
>>> t = []
>>> for a in s:
for b in a:
t.append(b)
>>> t
['a', 'b']