【发布时间】:2021-01-17 17:30:43
【问题描述】:
假设我有一个路径集字典:
my_dict['some_key'] = {'abc/hi/you','xyz/hi/you','jkl/hi/you'}
我想看看这个集合中是否出现了路径。如果我有完整的路径,我只需执行以下操作:
str = 'abc/hi/you'
if str in my_dict['some_key']:
print(str)
但如果我不知道b 是介于a 和c 之间的内容怎么办。如果它可以是字面上的任何东西怎么办。如果我是 ls 在 shell 中,我只会输入 * 并收工。
我想要做的是让 str 成为一个正则表达式:
regx = '^a.*c/hi/you$' #just assume this is the ideal regex. Doesn't really matter.
if regx in my_dict['some_key']:
print('abc/hi/you') #print the actual path, not the regx
什么是实现这种东西的简洁快速的方法?
【问题讨论】: