【发布时间】:2012-10-11 14:00:51
【问题描述】:
我正在转向 Python,但对 Python 方法还是比较陌生。我想编写一个函数,它接受一个字符串和一个列表,如果列表中的所有元素都出现在字符串中,则返回 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
另外,我希望有人可以在这里提出一种可能的正则表达式方法。我也在尝试它们。
【问题讨论】:
-
那么你可能会选择一个需要正则表达式的更好的问题。这没有。事实上你做得很好。
-
如果您想变得更好并尝试与正则表达式相关的任务,请尝试regex101.com/quiz
-
myfun = lambda a, b: all(i in a for i in b)