简体   繁体   English

将列表的python列表打印成字符串

[英]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)'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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