[英]Python: How to pass key to a dictionary from the variable of a function?
Basically I have two dictionaries: one is a Counter()
the other is dict()
基本上我有两个字典:一个是
Counter()
另一个是dict()
The first one contains all the unique words in a text, as keys and each words' frequency in the text, as the values 第一个包含文本中所有唯一的单词(作为键)和文本中每个单词的频率(作为值)
The second contains the same unique words as keys, but the values are the definitions which are user-inputted. 第二个包含与键相同的唯一单词,但是值是用户输入的定义。
The latter is what I'm having trouble implementing. 后者是我执行时遇到的困难。 I created a function which takes in a word, checks if that word is in the frequency dictionary, and if it is, allows the user to input a definition of that word (else, it will print an error).
我创建了一个函数,该函数接收一个单词,检查该单词是否在频率词典中,如果存在,则允许用户输入该单词的定义(否则,它将显示错误)。 The word and its definition are then added to the second dictionary as a key-value pair (using
dict.update(word=definition)
). 然后,将单词及其定义作为键值对添加到第二个字典中(使用
dict.update(word=definition)
)。
But whenever I run the program I get the error: 但是,每当我运行程序时,都会出现错误:
Nameerror: name '' is not defined NameError:名称“”未定义
Here is the code: 这是代码:
import string
import collections
import pickle
freq_dict = collections.Counter()
dfn_dict = dict()
def cleanedup(fh):
for line in fh:
word = ''
for character in line:
if character in string.ascii_letters:
word += character
else:
yield word
word = ''
def process_book(textname):
with open (textname) as doc:
freq_dict.update(cleanedup(doc))
global span_freq_dict
span_freq_dict = pickle.dumps(freq_dict)
def show_Nth_word(N):
global span_freq_dict
l = pickle.loads(span_freq_dict)
return l.most_common()[N]
def show_N_freq_words(N):
global span_freq_dict
l = pickle.loads(span_freq_dict)
return l.most_common(N)
def define_word(word):
if word in freq_dict:
definition = eval(input('Please define ' + str(word) + ':'))
dfn_dict({word: definition})
else:
return print('Word not in dictionary!')
process_book('DQ.txt')
process_book('HV.txt')
# This was to see if the if/else was working
define_word('asdfs')
#This is the actual word I want to add
define_word('de')
print(dfn_dict.items())
I get the feeling that either the error is very small or very big. 我感觉到错误要么很小要么很大。 Any help would be greatly appreciated.
任何帮助将不胜感激。
EDIT: So the program now allows me to enter a definition, but returns this error once I do so: 编辑:所以该程序现在允许我输入一个定义,但是一旦执行此操作,就会返回此错误:
>>>
Word not in dictionary!
Please define esperar:To await
Traceback (most recent call last):
File "C:\Users\User 3.1\Desktop\Code Projects\dict.py", line 50, in <module>
define_word('esperar')
File "C:\Users\User 3.1\Desktop\Code Projects\dict.py", line 37, in define_word
definition = eval(input('Please define ' + str(word) + ':'))
File "<string>", line 1
To await
^
SyntaxError: unexpected EOF while parsing
>>>
dict.update(word=definition)
won't do what you think it does. dict.update(word=definition)
不会执行您认为的操作。 See this example: 请参阅以下示例:
>>> someDict = {}
>>> word = 'foo'
>>> definition = 'bar'
>>> someDict.update(word=definition)
>>> someDict
{'word': 'bar'}
As you can see, this method will always update the key word
although you want the word
variable to be resolved first. 正如你所看到的,这种方法将总是更新键
word
虽然你想要的word
变量首先须解决。 This won't work though because you are passing a named argument to the update
function, and those named arguments are taken literally. 但是,这将不起作用,因为您要将命名参数传递给
update
函数,而这些命名参数是按字面意义使用的。
If you want to update the key that equals to the value of word
, you can just do it like this: 如果要更新等于
word
的值的键,则可以这样进行:
someDict[word] = definition
dfn_dict.update(word = definition)
is equivalent to dfn_dict.update({"word": definition})
. dfn_dict.update(word = definition)
等效于dfn_dict.update({"word": definition})
。 You want to use dfn_dict.update({word: definition})
. 您要使用
dfn_dict.update({word: definition})
。
input() actually evaluates the input as Python code. input()实际上将输入评估为Python代码。 Use
raw_input
instead. 请改用
raw_input
。
def define_word(word):
if word in freq_dict:
definition = raw_input('Please define ' + str(word) + ':')
global dfn_dict
return dfn_dict.update({word: definition})
else:
print('Word not in dictionary!')
process_book('DQ.txt')
# This was to see if the if/else was working
define_word('asdfs')
#This is the actual word I want to add
define_word('esperar')
print(dfn_dict.items())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.