1

我有列表列表:

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

没有运气。

4

1 回答 1

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)'

或者

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)'
于 2012-12-01T03:15:01.670 回答