有没有办法改变这个字典的输出?
Is there a way to change this dictionary output?
更改我的词典,这是初始代码:
bow=[[i for i in all_docs[j] if i not in stopwords] for j in range(n_docs)]
bow=list(filter(None,bow))
bow
这里是弓输出:
[['lunar',
'satellite',
'needs'],
['glad',
'see',
'griffin'] ]
worddict_two = [ (i,key) for i,key in enumerate(bow)]
worddict_two
来自这个输出:
[(0,
['lunar',
'satellite',
'needs']),
(1,
['glad',
'see',
'griffin'])
到此输出:
[(0,'lunar satellite needs'),
(1,'glad see griffin') ) ]
你可以这样做:
bow = [
['lunar','satellite','needs'],
['glad','see','griffin']
]
res = [(i,*key) for i,key in enumerate(bow)]
print(res)
worddict_two = [ (i, " ".join(key)) for i,key in enumerate(bow)]
这行得通。使用 join 将元组中的所有项目连接成一个字符串,以 space 作为分隔符
您可以像这样用空格加入列表
worddict_two = [ (i,' '.join(key)) for i,key in enumerate(bow)]
试试这个:
word_three = [(item[0], ', '.join(word for word in item[1])) for item in worddict_two]
更改我的词典,这是初始代码:
bow=[[i for i in all_docs[j] if i not in stopwords] for j in range(n_docs)]
bow=list(filter(None,bow))
bow
这里是弓输出:
[['lunar',
'satellite',
'needs'],
['glad',
'see',
'griffin'] ]
worddict_two = [ (i,key) for i,key in enumerate(bow)]
worddict_two
来自这个输出:
[(0,
['lunar',
'satellite',
'needs']),
(1,
['glad',
'see',
'griffin'])
到此输出:
[(0,'lunar satellite needs'),
(1,'glad see griffin') ) ]
你可以这样做:
bow = [
['lunar','satellite','needs'],
['glad','see','griffin']
]
res = [(i,*key) for i,key in enumerate(bow)]
print(res)
worddict_two = [ (i, " ".join(key)) for i,key in enumerate(bow)]
这行得通。使用 join 将元组中的所有项目连接成一个字符串,以 space 作为分隔符
您可以像这样用空格加入列表
worddict_two = [ (i,' '.join(key)) for i,key in enumerate(bow)]
试试这个:
word_three = [(item[0], ', '.join(word for word in item[1])) for item in worddict_two]