List 要字符串的列表的列表
我有一个列表,例如:List 要字符串的列表的列表,list,python-2.7,List,Python 2.7,我有一个列表,例如: sample = [['a'],['b']] 我希望将此列表转换为字符串列表,如 sample = ['a','b'] 我以前使用过列表,但这似乎困扰着我!! 提前感谢您尚未涵盖所有案例,但: [''.join(x) for x in sample] 试试这个: In [5]: sample = [['a'],['b']] In [6]: [item for sublist in sample for item in sublist] Out[6]: ['a', '
sample = [['a'],['b']]
我希望将此列表转换为字符串列表,如
sample = ['a','b']
我以前使用过列表,但这似乎困扰着我!!
提前感谢您尚未涵盖所有案例,但:
[''.join(x) for x in sample]
试试这个:
In [5]: sample = [['a'],['b']]
In [6]: [item for sublist in sample for item in sublist]
Out[6]: ['a', 'b']
或
简单的for循环就足够了-
>>> s = [['a'],['b']]
>>> t = []
>>> for a in s:
for b in a:
t.append(b)
>>> t
['a', 'b']
>>> s = [['a'],['b']]
>>> t = []
>>> for a in s:
for b in a:
t.append(b)
>>> t
['a', 'b']