我正在转向 Python,并且对 Pythonic 方法还比较陌生。我想编写一个函数,它接受一个字符串和一个列表,如果列表中的所有元素都出现在字符串中,则返回 true。
这看起来相当简单。然而,我面临着一些困难。代码是这样的:
def myfun(str,list):
for a in list:
if not a in str:
return False
return True
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
另外,我希望有人可以在这里建议一种可能的正则表达式方法。我也在尝试用我的双手。
>>> all(x in 'tomato' for x in ['t','o','m','a'])
True
>>> all(x in 'potato' for x in ['t','o','m','a'])
False
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)