不区分大小写的字典字符串搜索 [英] Case insensitive string search of dictionary

查看:78
本文介绍了不区分大小写的字典字符串搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索一个大词典以查找特定的字符串.字典的键是数字,然后值是元组.我如何创建一个不区分大小写的搜索循环遍历字典的函数,然后获取包含相关短语的键,并将其添加到新列表中?我想在我创建的用于打印信息的后续功能(显示)中使用此新列表[match].

I have a large dictionary that I am searching through to find a specific string. The keys for the dictionary are numbers and then the values are tuples. How would I create a function to loop through the dictionary using a case-insensitive search then take the keys that contain the relevant phrase, and add them to a new list? I would like to use this new list [match] in a subsequent function (show) that I have created to print the information.

我的代码如下:

dict = {
1 : (value,value,value),
2 : (value,value,value),
so on...
}
# searches dict, criteria determines whether search is for str() or int(), phrase is string I am searching for
def search(criteria,phrase):

    enter code here

# prints new list
def show(match):

推荐答案

您将要使用


功能:


As a function:

def search(criteria, phrase):
    return [i for i, j in criteria.items() if phrase in j]

这篇关于不区分大小写的字典字符串搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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