简体   繁体   中英

print python list of lists into string

I have list of lists:

[['lorem', ['a', 'b', 'c', 'd']], ['ipsum', ['e']], ['dolor', ['f','g']], ['sit', ['h', 'i']]]

how to achieve this one?

lorem (a + b + c + d); ipsum (e); dolor (e + g); sit (h + i)

So far, I've tried:

print '\n'.join('%s %s' % x for x in list) 

with no luck.

In [14]: L = [['lorem', ['a', 'b', 'c', 'd']], ['ipsum', ['e']], ['dolor', ['f', 'g']], ['sit', ['h', 'i']]]

In [15]: '; '.join("%s (%s)" %(elem[0], " + ".join(elem[1])) for elem in L)
Out[15]: 'lorem (a + b + c + d); ipsum (e); dolor (f + g); sit (h + i)'

OR

In [18]: L = [(e[0], " + ".join(e[1])) for e in L]

In [19]: L
Out[19]: 
[('lorem', 'a + b + c + d'),
 ('ipsum', 'e'),
 ('dolor', 'f + g'),
 ('sit', 'h + i')]

In [20]: '; '.join("%s (%s)" %(e[0], e[1]) for e in L)
Out[20]: 'lorem (a + b + c + d); ipsum (e); dolor (f + g); sit (h + i)'

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