简体   繁体   中英

How to split strings within a list into separate strings

This is a list I have.

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

How to I split the individual subsections of the list into something else that looks like:

x = [['E'], [4], ['F'], [1]]
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']]

It looks like the kind of question that might be easier to answer with more context. But still, how about:

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]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM