检查字符串列表是否包含另一个字符串(Python)的所有字符
我想得到“tori”和“tutorial”,因为这两个词包含检查字符串列表是否包含另一个字符串(Python)的所有字符,python,substring,Python,Substring,我想得到“tori”和“tutorial”,因为这两个词包含o,t,I。上面的代码将打印列表中的所有内容。使用find()不是一个选项,因为没有匹配项 在这里使用match是一个好的选择吗?我需要一些提示。提前感谢。用于会员资格测试 S = ['cat','tor','tutorial','item','aba','tori'] T = "oti" for item in S: for i in item: if i in T: print
o
,t
,I
。上面的代码将打印列表中的所有内容。使用find()不是一个选项,因为没有匹配项
在这里使用match是一个好的选择吗?我需要一些提示。提前感谢。用于会员资格测试
S = ['cat','tor','tutorial','item','aba','tori']
T = "oti"
for item in S:
for i in item:
if i in T:
print(item)
如果存在重复字符,则此操作无效
>>>
>>> T = 'oti'
>>> S = ['cat','tor','tutorial','item','aba','tori']
>>> t = set(T)
>>> t
{'o', 't', 'i'}
>>> for thing in S:
... if t.issubset(thing):
print(thing)
tutorial
tori
>>>
使用
all
>>> q = set('otti')
>>> for thing in S:
... if q.issubset(thing):
print(thing)
tutorial
tori
>>>
>>>
>>> for thing in S:
... if all(c in thing for c in T):
print(thing)
tutorial
tori
>>>