python中提取字典中的键值

 
 1 # 字典如下
 2 movie = {
 3     '妖猫传':['',''],
 4     '无问西东':['',''],
 5     '超时空':['','']
 6 }
 7 name = input('请输入演员的名字')  # 收集信息
 8 for i in movie:         # 遍历字典movie的键名
 9     actor = movie[i]    # 设置一变量actor等于遍历的字典值
10     if name in actor:   # 如果输入的演员名字在遍历的值中
11         print(name + '出演了'+i)  # 打印 name(演员名) ,i为键名
 

结果如下:

请输入演员的名字雷
雷出演了超时空

Process finished with exit code 0

 

猜你喜欢

转载自www.cnblogs.com/Through-Target/p/12096302.html