我有列表清单:
[['lorem', ['a', 'b', 'c', 'd']], ['ipsum', ['e']], ['dolor', ['f','g']], ['sit', ['h', 'i']]]
如何实现这一目标?
lorem (a + b + c + d); ipsum (e); dolor (e + g); sit (h + i)
到目前为止,我已经尝试过了:
print '\n'.join('%s %s' % x for x in list)
没有运气。
答案 0 :(得分:2)
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)'