我的输出格式为
[('the', 1334),
('and', 919),
('a', 615),
('to', 560),
('i', 519),
('in', 317),
('he', 299),
('they', 287),
('was', 277),
('it', 276),
('of', 267),
('on', 214),
('you', 210),
('went', 206)]
我的问题是如何为给定单词选择值。例如,我的函数如下所示:
def unigram(word):
u = [' '.join(w).lower() for w in train_corrected] # I want to change train corrected to a list of sentences so I can lower every character
u = ' '.join(u) # makes the list of sentences into one giant string
u = nltk.word_tokenize(u) # converts the giant string back into a list of single word elements
freq = Counter(u).most_common() # https://docs.python.org/3/library/collections.html
return freq # we only want the result of the word we input into our function not the entire dict of freq
我想回来说
unigram('the') # The output of which would be 1334 from the list of tuples
有人可以指出我的正确方向吗?
答案 0 :(得分:0)
我将使用列表理解:
def unigram(word):
return [x[1] for x in train_corrected if x[0] == word][0]
然后致电:
print(unigram('the'))
答案 1 :(得分:0)
下面的代码块可以为您提供帮助。 Required_string是您要根据问题中显示的元组列表为其关联值的字符串。
Required_string = 'the'
For I in output:
If I[0] == required_string:
Print(I[1])
答案 2 :(得分:0)
使用@Patrick Artner的答案达到了预期的结果。谢谢男人
def unigram(word):
u = [' '.join(w).lower() for w in train_corrected] # I want to change train corrected to a list of sentences so I can lower every character
u = ' '.join(u) # makes the list of sentences into one giant string
u = nltk.word_tokenize(u) # converts the giant string back into a list of single word elements
freq = Counter(u).most_common() # https://docs.python.org/3/library/collections.html
return return Counter(u).get(word,0) # we only want the result of the word we input into our function not the entire dict of freq
print(unigram('the')) # output = 1334 as expected