简体   繁体   中英

python nested list comprehension string concatenation

I have a list of lists in python looking like this:

[['a', 'b'], ['c', 'd']]

I want to come up with a string like this:

a,b;c,d

So the lists should be separated with a ; and the values of the same list should be separated with a ,

So far I tried ','.join([y for x in test for y in x]) which returns a,b,c,d . Not quite there, yet, as you can see.

";".join([','.join(x) for x in a])
>>> ';'.join(','.join(x) for x in [['a', 'b'], ['c', 'd']])
'a,b;c,d'

To do it functionally you could use map:

l = [['a', 'b'], ['c', 'd']]


print(";".join(map(".".join, l)))
a.b;c.d

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