【发布时间】:2016-08-12 18:18:45
【问题描述】:
假设我有一个列表match = ['one', 'two', 'three']
我有一个参数列表foo = ['something', 'something_two', 'something', (...)]
我只想对与match 列表中的任何项目匹配的项目进行操作:
for each in foo:
for match_item in match:
if match_item not in each:
no_match = True
break
if no_match:
break
# do the desired operations
但我不知道如何做到这一点,以便当'something_two' 遇到'one' 并打破所有循环时它不会失败。 match 中的项目将只是 foo 项目中整个字符串的一部分,这就是我循环遍历 match 中的项目列表的原因。
解决这个问题的好方法是什么?
【问题讨论】:
-
你不能只做
if each in match: # do desired op吗?
标签: python-2.7 search string-matching