如果我们将任意键作为输入,我们应该得到相应的值作为输出;如果我们将列表中的任意一个元素作为输入,我们应该得到相应的键作为输出。请忽略中间的enter code here
以下是键及其值
"RCB": ["Virat";Abd", "Maxwell","Chahal"],"CSK"; [" doni ", "Raina", "Jadeja", "Faf"]["Morgan", "Russell", "Karthik", "Naenter code here
ine"]"PBKS":【"Rahul","Mayank","Shami","Gayle"]
我相信这就是你想要的:
dict = {
"RCB" : ["Virat", "Abd", "Maxwell","Chahal"],
"CSK": ["Dhoni", "Raina", "Jadeja", "Faf"],
"KKR": ["Morgan", "Russell", "Karthik", "Naenter code hereine"],
"PBKS" : ["Rahul", "Mayank", "Shami", "Gayle"]
}
def getOutput(dict, value):
try:
output = dict[value]
return output
except KeyError:
for key in dict:
if value in dict[key]:
output = key
return output
return None
print(getOutput(dict, "RCB"))
#["Virat", "Abd", "Maxwell","Chahal"]
print(getOutput(dict, "Raina"))
#"CSK"