我在一个列表中有两个列表:
t = [['this','is','a','sentence'],['Hello','I','am','Daniel']]
我想把它们组合起来,得到两个句子:
'this is a sentence. Hello I am Daniel.'
我很快想出了以下解决方案:
text = ''
for sent in t:
text = text + ' '.join(sent) + '. '
但也许有更可读(风格更好(的解决方案来完成这项任务?
您可以将其缩短为一个列表理解,并在末尾添加另一个".":
text = '. '.join(' '.join(sent) for sent in t) + '.'
您也可以使用list comprehensions
:
In [584]: ''.join([' '+ ' '.join(sent) + '. ' for sent in t])
Out[584]: ' this is a sentence. Hello I am Daniel. '