如何将列表中的字符串拆分为单独的字符串

时间:2015-11-18 02:17:48

标签: python python-3.x

这是我的名单。

x = [['E,4'], ['F,1']]

如何将列表的各个子部分拆分为其他类似的部分:

x = [['E'], [4], ['F'], [1]]

2 个答案:

答案 0 :(得分:2)

In [23]: x = [['E,4'], ['F,1']]

In [24]: [[e] for sub in x for s in sub for e in s.split(',')]
Out[24]: [['E'], ['4'], ['F'], ['1']]

答案 1 :(得分:1)

看起来这种问题可能更容易回答更多的背景。但是,如何:

L = [['E,4'], ['F,1']]

output = []
for pair in L:
    x, y = pair[0].split(',')
    output.append([x])
    output.append([int(y)])

>>>output
[['E'], [4], ['F'], [1]]