Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从元组列表中提取元素_Python_Python 3.x_List_Tuples_Nltk - Fatal编程技术网

Python 从元组列表中提取元素

Python 从元组列表中提取元素,python,python-3.x,list,tuples,nltk,Python,Python 3.x,List,Tuples,Nltk,我有一个表单中的输出 [('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 un

我有一个表单中的输出

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

有人能给我指出正确的方向吗?

我将使用列表:

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

返回计数器(u)。获取(word,0)
-如果您多次查询此项,则应缓存计数器结果。谢谢Patrick,这是有效的。嗨,Wasif,谢谢,但它不起作用。我收到“列表索引超出范围”错误