通过字典中的值获取键值

编程入门 行业动态 更新时间:2024-10-27 16:27:56
本文介绍了通过字典中的值获取键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我做了一个函数,它将在字典中查找年龄并显示匹配的名称:

I made a function which will look up ages in dictionary and show the matching name:

list = {'george':16,'amber':19} search_age = raw_input("Provide age") for age in list.values(): if age == search_age: name = list[age] print name

我知道如何比较找到我的年龄只是不知道如何显示这个人的名字。另外,由于第5行,我得到一个 KeyError 。我知道这是不正确的,但我无法确定使其向后搜索。

I know how to compare and find the age I just don't know how to show the name of the person. Additionally, I am getting a KeyError because of line 5. I know it's not correct but I can't figure out to make it search backwards.

推荐答案

没有。 dict 不是以这种方式使用。

There is none. dict is not intended to be used this way.

for name, age in list.iteritems(): if age == search_age: print name

更多推荐

通过字典中的值获取键值

本文发布于:2023-05-29 15:07:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/347286.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:键值   字典

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!