gpt4 book ai didi

字符串中的 Python 通配符搜索

转载 作者:IT老高 更新时间:2023-10-28 20:22:32 24 4
gpt4 key购买 nike

假设我有一个列表

list = ['this','is','just','a','test']

如何让用户进行通配符搜索?

搜索词:'th_s'

将返回“这个”

最佳答案

使用 fnmatch :

import fnmatch
lst = ['this','is','just','a','test']
filtered = fnmatch.filter(lst, 'th?s')

如果您想允许 _ 作为通配符,只需 replace所有带有 '?'(一个字符)或 *(多个字符)的下划线。

如果您希望您的用户使用更强大的过滤选项,请考虑允许他们使用 regular expressions .

关于字符串中的 Python 通配符搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11427138/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com