我有一个看起来像这样的元组列表:
[('this', 'is'), ('is', 'the'), ('the', 'first'), ('first', 'document'), ('document', '.')]
Run Code Online (Sandbox Code Playgroud)
什么是最pythonic和有效的方式转换为每个令牌由空格分隔:
['this is', 'is the', 'the first', 'first document', 'document .']
Run Code Online (Sandbox Code Playgroud)
Igo*_*bin 13
非常简单:
[ "%s %s" % x for x in l ]
Run Code Online (Sandbox Code Playgroud)
使用map()和join():
tuple_list = [('this', 'is'), ('is', 'the'), ('the', 'first'), ('first', 'document'), ('document', '.')]
string_list = map(' '.join, tuple_list)
Run Code Online (Sandbox Code Playgroud)
正如inspectorG4dget指出的那样,列表推导是这样做的最pythonic方式:
string_list = [' '.join(item) for item in tuple_list]
Run Code Online (Sandbox Code Playgroud)