这里有点奇怪,虽然我确定我刚刚转换为Python时做了些傻事。我有dict
dict
个dict
(我知道有人不喜欢这样),代码如下:
import sys
hash = {}
def update_official():
for keyv in hash:
for word in hash[keyv]:
if word == 'Alice':
hash[keyv][word]["official"] = 1
else:
hash[keyv][word]["official"] = 0
def add_case_terms():
new_terms = {}
for term_id in hash:
for term in dict[term_id]:
if term_id not in new_terms:
new_terms[term_id] = {}
new_terms[term_id][term.title()] = hash[term_id][term]
new_terms[term_id][term.upper()] = hash[term_id][term]
new_terms[term_id][term.lower()] = hash[term_id][term]
for term_id in new_terms:
for term in new_terms[term_id]:
hash[term_id][term] = new_terms[term_id][term]
def main():
key1 = 1
words = ['Alice','John']
for word in words:
if key1 not in dict:
hash[key1] = {}
hash[key1][word] = {"official":0,"other_info":5}
add_case_terms()
update_official()
for key in hash:
for term in hash[key]:
print str(term) + " has official: " + str(hash[key][term]["official"])
print "\nAmending 'alice'.\n"
hash[1]['alice']["official"] = 1
for key in hash:
for term in hash[key]:
print str(term) + " has official: " + str(hash[key][term]["official"])
if __name__ == "__main__":
sys.exit(main())
我得到的输出是:
john has official: 0
Alice has official: 0
ALICE has official: 0
John has official: 0
JOHN has official: 0
alice has official: 0
Amending 'alice'.
john has official: 0
Alice has official: 1
ALICE has official: 1
John has official: 0
JOHN has official: 0
alice has official: 1
我不明白为什么每个alice
密钥都会更新,而不仅仅是对应正确案例的密钥。有什么想法吗?
答案 0 :(得分:1)
兴趣点。
不要通过已存在的名称调用变量。 dict()
是内置函数,不应用作变量名。尝试一些简单的事情my_dict
您不需要嵌套循环来从dict获取键/值对;使用.items()返回两者的元组:
for key, value in my_dict.items():
print key, value
如果这是你的目标 - 制作敏感词典的案例很简单!你过度思考它;)
您可以使用getter和setter,如:
def set_insensitive(my_dict, str_key, my_val):
my_dict[str_key.lower()] = myval
def get_insensitive(my_dict, str_key):
return my_dict[str_key.lower()]
你可以从一个区分大小写的dict中创建一个新的不区分大小写的dict:
def create_insensitive(my_dict):
new_dict = {}
for str_key, value in my_dict.items():
new_dict[str_key.lower()] = value
return new_dict
或者理解:
def create_insensitive(my_dict):
return = { str_key.lower() : value for str_key, value in my_dict.items() }
答案 1 :(得分:1)
这只是因为你有相同的可变对象的多个键。你的结构就像:
value = {"official":0,"other_info":5}
hash[1]['ALICE'] = value
hash[1]['alice'] = value
hash[1]['Alice'] = value
请注意,所有这些分配都使用具有不同键的相同value
。稍后,当您执行hash[keyv][word]["official"] = X
时,使用word
的各种值并不重要,因为它们会导致相同的对象。
new_terms[term_id][term.title()] = hash[term_id][term]
new_terms[term_id][term.upper()] = hash[term_id][term]
new_terms[term_id][term.lower()] = hash[term_id][term]
是代码中创建各种键的部分,所有键都指向同一个对象。
答案 2 :(得分:0)
感谢您的帮助。事实证明,在这种情况下,重要的是不要影响字典。解决方案如下:
import sys
my_dict = {}
def update_official():
for keyv in my_dict:
for word in my_dict[keyv]:
if word == 'Alice':
my_dict[keyv][word]["official"] = 1
else:
my_dict[keyv][word]["official"] = 0
def add_case_terms():
new_terms = {}
for term_id,val in my_dict.items():
for term in my_dict[term_id]:
if term_id not in new_terms:
new_terms[term_id] = {}
new_terms[term_id][term.title()] = dict(my_dict[term_id][term])
new_terms[term_id][term.upper()] = dict(my_dict[term_id][term])
new_terms[term_id][term.lower()] = dict(my_dict[term_id][term])
for term_id in new_terms:
for term in new_terms[term_id]:
my_dict[term_id][term] = dict(new_terms[term_id][term])
def set_insensitive(my_dict, str_key, my_val):
my_dict[str_key.lower()] = myval
my_dict[str_key.upper()] = myval
my_dict[str_key.title()] = myval
def main():
key1 = 1
words = ['Alice','John']
for word in words:
if key1 not in my_dict:
my_dict[key1] = {}
my_dict[key1][word] = {"official":0,"other_info":5}
add_case_terms()
update_official()
for key in my_dict:
for term in my_dict[key]:
print str(term) + " has official: " + str(my_dict[key][term]["official"])
print "\nAmending 'alice'.\n"
my_dict[1]['alice']["official"] = 1
for key in my_dict:
for term in my_dict[key]:
print str(term) + " has official: " + str(my_dict[key][term]["official"])
if __name__ == "__main__":
sys.exit(main())

谢谢大家。