我有一个包含多个列表的列表,我想将这些列表合并为单个字符串,以便可以在其上使用counter()方法。
样品清单 清单1 = [ “这是第一个文件”, “本文件是第二份文件”, “这是第三个”, “这是第一个文档”,
需要输出“这是第一个文件,这个文件是第二个文件,这是第三个文件,这是第一个文件”
谢谢。
使用.join()方法:
list1= ['this is the first document', 'this document is the second document', 'and this is the third one', 'is this the first document']
list1_joined = ",".join(list1)
print(list1_joined)
#Output:
'this is the first document,this document is the second document,and this is the third one,is this the first document'
您可以使用内置的join()
功能:
list = [ 'this is the first document', 'this document is the second document', 'and this is the third one', 'is this the first document']
print(', '.join(list))
输出:
this is the first document this document is the second document and this is the third one is this the first document
创建一个计数器对象,并使用object_name.element()遍历并打印。
c = Counter(List1)
for i in c.elements():
print ( i, end = " ")
有关详细信息,[https://www.geeksforgeeks.org/python-counter-objects-elements/][1]