[英]Compare one Dictionary Key with another Dictionary Value and create a new Dictionary
[英]Randomly print a dictionary key, then compare its value with another dictionary
我在从字典中打印随机键时遇到问题。 使用 dict_2 的随机性,一切正常,并且 function 也可以正常工作,但是使用dict_1
的随机性有问题。
这段代码的目的是随机 select 的dict_1
和dict_2
的键,使用 function 进行语法“比较” (dict_1 和 dict_2 的键必须具有相同的性别和单位),然后打印出来。 function 工作正常
重要提示:我不能使用与dict_2
相同的代码,否则我将获得一个没有其值的键,因此无法使用 get_example function 将其与dict_2
“比较”。 即使使用list(dict_1.items())
而不是list(dict_1.keys())
,我也无法将值与其他字典进行比较。 相反,目前,这些值已正确比较,但dict_1
没有随机化。 我做了几次尝试,但我得到了很多不同的错误,太多不同的错误。
如何随机获取dict_1
密钥? 我总是只打印 dict_1 的“archiviato”或“ dict_1
”键,但我想在 dict_1 的所有键中随机打印一个。 我知道解决方案很简单,但我做不到。 代码的所有 rest 工作正常。 谁能帮我?
为了简洁明了,我已经拆分了字典和代码,但下面是完整的代码。
这是我的小代码:
import random
#Random Dict_2
x = list(dict_2.keys())
x_random = random.choice(x)
#Relate the grammar of the two dictionaries to find the correct word
def get_example(x_random):
for key, value in dict_1.items():
if dict_2[x_random] == value:
return key
function = get_example(x_random)
test = f"{function} {x_random}"
print(test)
这是字典:
dict_1 = {
#Male
"archiviato " : {
"genere" : "maschile",
"unità" : "singolare",
},
"gestito " : {
"genere" : "maschile",
"unità" : "singolare",
},
"smarrito " : {
"genere" : "maschile",
"unità" : "singolare",
},
#Female
"archiviata " : {
"genere" : "femminile",
"unità" : "singolare",
},
"gestita " : {
"genere" : "femminile",
"unità" : "singolare",
},
"smarrita " : {
"genere" : "femminile",
"unità" : "singolare",
},
}
dict_2 = {
#Male
"il documento" : {
"genere" : "maschile",
"unità" : "singolare",
},
"il foglio" : {
"genere" : "maschile",
"unità" : "singolare",
},
"lo scatolone" : {
"genere" : "maschile",
"unità" : "singolare",
},
#Female
"la cartella" : {
"genere" : "femminile",
"unità" : "singolare",
},
"la borsa" : {
"genere" : "femminile",
"unità" : "singolare",
},
"la chiave" : {
"genere" : "femminile",
"unità" : "singolare",
},
}
这是完整的代码:
import random
dict_1 = {
#Maschili
"archiviato " : {
"genere" : "maschile",
"unità" : "singolare",
},
"gestito " : {
"genere" : "maschile",
"unità" : "singolare",
},
"smarrito " : {
"genere" : "maschile",
"unità" : "singolare",
},
#Femmili
"archiviata " : {
"genere" : "femminile",
"unità" : "singolare",
},
"gestita " : {
"genere" : "femminile",
"unità" : "singolare",
},
"smarrita " : {
"genere" : "femminile",
"unità" : "singolare",
},
}
dict_2 = {
#Maschili
"il documento" : {
"genere" : "maschile",
"unità" : "singolare",
},
"il foglio" : {
"genere" : "maschile",
"unità" : "singolare",
},
"lo scatolone" : {
"genere" : "maschile",
"unità" : "singolare",
},
#Femminili
"la cartella" : {
"genere" : "femminile",
"unità" : "singolare",
},
"la borsa" : {
"genere" : "femminile",
"unità" : "singolare",
},
"la chiave" : {
"genere" : "femminile",
"unità" : "singolare",
},
}
#Random Dict_2
x = list(dict_2.keys())
x_random = random.choice(x)
#Relate the grammar of the two dictionaries to find the correct word
def get_example(x_random):
for key, value in dict_1.items():
if dict_2[x_random] == value:
return key
function = get_example(x_random)
test = f"{function} {x_random}"
print(test)
我建议不要在找到匹配项后立即返回,而是建议跟踪所有匹配项,然后随机选择其中一个匹配项。
例子:
import random
#Random Dict_2
x = list(dict_2.keys())
x_random = random.choice(x)
#Relate the grammar of the two dictionaries to find the correct word
def get_example(x_random):
matches = []
for key, value in dict_1.items():
if dict_2[x_random] == value:
matches.append(key)
# If matches has no elements here, this will cause an error
return random.choice(matches)
function = get_example(x_random)
test = f"{function} {x_random}"
print(test)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.