我正在转向Python,而且对pythonic方法仍然相对较新.我想编写一个带字符串和列表的函数,如果列表中的所有元素都出现在字符串中,则返回true.
这似乎很简单.但是,我面临一些困难.代码如下:
def myfun(str,list):
for a in list:
if not a in str:
return False
return True
Run Code Online (Sandbox Code Playgroud)
Example : myfun('tomato',['t','o','m','a']) should return true
myfun('potato',['t','o','m','a']) should return false
myfun('tomato',['t','o','m']) should return true
Run Code Online (Sandbox Code Playgroud)
此外,我希望有人可以在这里提出可能的正则表达式方法.我也在试试他们.
Ign*_*ams 12
>>> all(x in 'tomato' for x in ['t','o','m','a'])
True
>>> all(x in 'potato' for x in ['t','o','m','a'])
False
Run Code Online (Sandbox Code Playgroud)