0

我有一个列表,例如:

sample = [['a'],['b']]

我希望将此列表转换为字符串列表,例如

sample = ['a','b']

我以前使用过列表,但这似乎困扰着我!提前致谢

4

3 回答 3

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']
于 2013-05-22T08:38:24.413 回答
0

您尚未涵盖所有案例,但是:

[''.join(x) for x in sample]
于 2013-05-22T08:38:21.747 回答
0

简单的 for 循环就足够了 -

>>> s = [['a'],['b']]
>>> t = []
>>> for a in s:
        for b in a:
            t.append(b)


>>> t
['a', 'b']
于 2013-05-22T08:43:33.177 回答