从元组列表中提取元素

Element extraction from a list of tuples

我有一个输出形式

[('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)]

我的问题是如何 select 给定单词的值。例如,我的函数如下所示:

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

我想return说

unigram('the') # The output of which would be 1334 from the list of tuples

谁能给我指出正确的方向?

我将使用列表理解:

def unigram(word):
  return [x[1] for x in train_corrected if x[0] == word][0]

并像这样打电话:

print(unigram('the'))

下面的代码块可以帮到你。 Required_string 是您想要根据问题中显示的元组列表为其关联值的字符串。

Required_string = 'the'
For I in output:
    If I[0] == required_string:
         Print(I[1]) 

使用@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