我无法将多个列表合并到一个文档中 这样的文件示例
['universitas', 'trunojoyo']
['trunojoyo', 'ada', 'diklat', 'bumi']
['roadshow', 'speedy', 'trunojoyo', 'isi']
我用这段代码尝试过,但是它使撕裂破裂了
' '.join(str(x)for x in documents)
' '.join(map(str, documents))
我想要这样的结果
['universitas trunojoyo', 'trunojoyo ada diklat bumi', 'roadshow speedy trunojoyo isi']
答案 0 :(得分:1)
我无法确定您要获取列表或字符串,因此以下实现可以同时实现。
input_lists = [['universitas', 'trunojoyo'],
['trunojoyo', 'ada', 'diklat', 'bumi'],
['roadshow', 'speedy', 'trunojoyo', 'isi']]
output_list = []
output_string = ""
for one_list in input_lists:
for one_element in one_list:
output_list.append(one_element)
output_string = "%s %s" % (output_string, one_element)
print(output_list)
print(output_string)
输出:
>> python test.py
['universitas', 'trunojoyo', 'trunojoyo', 'ada', 'diklat', 'bumi', 'roadshow', 'speedy', 'trunojoyo', 'isi']
universitas trunojoyo trunojoyo ada diklat bumi roadshow speedy trunojoyo isi
答案 1 :(得分:0)
这是执行此操作的基本代码
documents=[['universitas', 'trunojoyo'],['trunojoyo', 'ada', 'diklat', 'bumi'] ['roadshow', 'speedy', 'trunojoyo', 'isi']]
documents=[" ".join(i) for i in documents]
print(documents)
output expected by you
['universitas trunojoyo', 'trunojoyo ada diklat bumi', 'roadshow speedy trunojoyo isi']
答案 2 :(得分:0)
仅一行
[' '.join(str(word) for word in doc) for doc in documents]
其中
documents = [
['universitas', 'trunojoyo'],
['trunojoyo', 'ada', 'diklat', 'bumi'],
['roadshow', 'speedy', 'trunojoyo', 'isi'],
]