我有corpus_test
然后我将他升级到用单词分割的列表。我需要有2dictionarys
从这个和len
的文字。问题是唯一值。我需要所有的,甚至是副本
corpus_test = 'cat dog tiger tiger tiger cat dog lion'
corpus_test = [[word.lower() for word in corpus_test.split()]]
word_counts = defaultdict(int)
for rowt in corpus_test:
for wordt in rowt:
word_counts[wordt] += 1
index_wordso = dict((i, word) for i, word in enumerate(rowt))
word_indexso = dict((word, i) for i, word in enumerate(rowt))
v_countso = len(index_wordso)
我的代码通过index_wordso
和v_countso
为我提供了正确的输出:
index_wordso
#{0: 'cat',
1: 'dog',
2: 'tiger',
3: 'tiger',
4: 'tiger',
5: 'cat',
6: 'dog',
7: 'lion'}
v_countso
#8
但是word_indexso
(逆dict
到index_wordso
)给了我不正确的输出:
word_indexso
#{'cat': 5, 'dog': 6, 'tiger': 4, 'lion': 7}
这只是给我最后的值,不是全部。我需要全部8个值
字典中的键是唯一的,值不是唯一的。它就像一本单词词典:一个单词可以有多个定义,但不能有多个单词列表
解决方法是使用元组列表:
导致
但是请记住,这不是一个查找表,因此您必须(以某种方式)遍历元素以查找特定元素
另一种方法是使用列表字典:
导致
可以使用例如
word_indexso["cat"]
来查找,以获得与该单词相关联的数字列表相关问题 更多 >
编程相关推荐