i have corpus_test
then i'm upgrade him to list with split by words. i need have 2 dictionarys
from this and len
of text words. problem is unique values. i need all of them, even duplicates.
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)
my code give me right outputs with index_wordso
and v_countso
:
index_wordso
#{0: 'cat',
1: 'dog',
2: 'tiger',
3: 'tiger',
4: 'tiger',
5: 'cat',
6: 'dog',
7: 'lion'}
v_countso
#8
but word_indexso
(inverse dict
to index_wordso
) give's me not right output:
word_indexso
#{'cat': 5, 'dog': 6, 'tiger': 4, 'lion': 7}
that's give me only last values, not all. i need all 8 values
Keys in a dictionary are unique, values are not. It's like a word dictionary: there can be multiple definitions of a word, but not multiple word listings.
A workaround is using a list of tuples:
corpus_test = 'cat dog tiger tiger tiger cat dog lion'
corpus_test = [word.lower() for word in corpus_test.split()]
print([(a,b) for (a, b) in zip(corpus_test, range(len(corpus_test)))])
which results in
[('cat', 0),
('dog', 1),
('tiger', 2),
('tiger', 3),
('tiger', 4),
('cat', 5),
('dog', 6),
('lion', 7)]
Keep in mind, though, that this is not a lookup table, and so you must loop through the elements (in some way) to find a speficic element.
Another method is to use a dictionary of lists:
from collections import defaultdict
word_indexso = defaultdict(list)
corpus_test = 'cat dog tiger tiger tiger cat dog lion'.split()
for index, word in enumerate(corpus_test):
word_indexso[word].append(index)
print(word_indexso)
which results in
defaultdict(<class 'list'>, {'cat': [0, 5], 'dog': [1, 6], 'tiger': [2, 3, 4], 'lion': [7]})
which can be looked up with eg word_indexso["cat"]
to get the list of numbers associated with the word.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.