基于辅助列表中价值的字典中的最低值
我有一个列表和一个字典:
lst = ['Boston', 'Denver']
dic = {'Atlanta': 0, 'Boston':100, 'Denver':160}
我想找到具有最低值的字典键,只要密钥在列表中。在这种情况下,我想返回“波士顿”而不是“亚特兰大”,因为它不包含在列表中。如何有效地搜索最低价值?
I have a list and a dictionary:
lst = ['Boston', 'Denver']
dic = {'Atlanta': 0, 'Boston':100, 'Denver':160}
I want to find the dictionary key that has the lowest value provided the key is in the list. In this case, I want to return 'Boston' rather than 'Atlanta' since it is not contained in the list. How would I search for the minimum value efficiently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会这样做:
I would do this:
尝试:
打印:
Try:
Prints:
您可以将
min()
与一个键参数一起使用,该参数将inf
的值关联到列表中未显示的任何密钥:此输出:
You can use
min()
with a key parameter that associates a value ofinf
to any key that doesn't appear in the list:This outputs: