Python:在包含子字符串的字典中查找(字符串)键 [英] Python: Finding a (string) key in a dictionary that contains a substring

查看:935
本文介绍了Python:在包含子字符串的字典中查找(字符串)键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的脚本中,我建立了映射到艺术家(值)的键(专辑)字典,以便我可以快速查找艺术家制作什么专辑。但是,我希望用户能够找到包含子字符串的所有相册。例如,Light的搜索应该返回

In my script I build a dictionary of keys(albums) mapped to artists(values) so that I can do a quick lookup of what artists made what albums. However, I want the user to be able to find all albums which contain a substring. For example a search on "Light" should return

[Light Chasers] = Cloud Cult [Night Light] = Au Revoir Simone

最好的方法是什么?我是否应该使用字典?

What's the best way to do this? Should I even be using a dictionary?

推荐答案

[(k, v) for (k, v) in D.iteritems() if 'Light' in k]

这篇关于Python:在包含子字符串的字典中查找(字符串)键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆