any() is by far the best approach if all you want is True or False, but if you want to know specifically which string/strings match, you can use a couple things.
If you want the first match (with False as a default):
match = next((x for x in a if x in str), False)
If you want to get all matches (including duplicates):
matches = [x for x in a if x in str]
If you want to get all non-duplicate matches (disregarding order):
matches = {x for x in a if x in str}
If you want to get all non-duplicate matches in the right order:
matches = []
for x in a:
if x in str and x not in matches:
matches.append(x)
You should be careful if the strings in a or str gets longer. The straightforward solutions take O(S*(A^2)), where S is the length of str and A is the sum of the lenghts of all strings in a. For a faster solution, look at Aho-Corasick algorithm for string matching, which runs in linear time O(S+A).
回答 3
只是为了增加一些多样性regex:
import re
if any(re.findall(r'a|b|c', str, re.IGNORECASE)):print'possible matches thanks to regex'else:print'no matches'
import re
if any(re.findall(r'a|b|c', str, re.IGNORECASE)):
print 'possible matches thanks to regex'
else:
print 'no matches'
or if your list is too long – any(re.findall(r'|'.join(a), str, re.IGNORECASE))
回答 4
您需要迭代a的元素。
a =['a','b','c']
str ="a123"
found_a_string =Falsefor item in a:if item in str:
found_a_string =Trueif found_a_string:print"found a match"else:print"no match found"
a = ['a', 'b', 'c']
str = "a123"
found_a_string = False
for item in a:
if item in str:
found_a_string = True
if found_a_string:
print "found a match"
else:
print "no match found"
Put it in the same directory as your main Python file and name it aho_corasick.py
Try the alrorithm with the following code:
from aho_corasick import aho_corasick #(string, keywords)
print(aho_corasick(string, ["keyword1", "keyword2"]))
Note that the search is case-sensitive
回答 6
a =['a','b','c']
str ="a123"
a_match =[Truefor match in a if match in str]ifTruein a_match:print"some of the strings found in str"else:print"no strings found in str"
a = ['a', 'b', 'c']
str = "a123"
a_match = [True for match in a if match in str]
if True in a_match:
print "some of the strings found in str"
else:
print "no strings found in str"
if any(your_required in yourinput for your_required in original_word ):
如果要在那个original_word中输入所有想要的输入,请使用所有简单的输入
original_word =['h','a','c','k','e','r','e','a','r','t','h']
yourinput = str(input()).lower()if all(requested_word in yourinput for requested_word in original_word):print("yes")
a = ['a', 'b', 'c']
str = "a123"
if set(a) & set(str):
print("some of the strings found in str")
else:
print("no strings found in str")
This works if a does not contain any multiple-character values (in which case use any as listed above). If so, it’s simpler to specify a as a string: a = 'abc'.
回答 10
flog = open('test.txt','r')
flogLines = flog.readlines()
strlist =['SUCCESS','Done','SUCCESSFUL']
res =Falsefor line in flogLines:for fstr in strlist:if line.find(fstr)!=-1:print('found')
res =Trueif res:print('res true')else:print('res false')
flog = open('test.txt', 'r')
flogLines = flog.readlines()
strlist = ['SUCCESS', 'Done','SUCCESSFUL']
res = False
for line in flogLines:
for fstr in strlist:
if line.find(fstr) != -1:
print('found')
res = True
if res:
print('res true')
else:
print('res false')
回答 11
我会使用这种功能来提高速度:
def check_string(string, substring_list):for substring in substring_list:if substring in string:returnTruereturnFalse
def check_string(string, substring_list):
for substring in substring_list:
if substring in string:
return True
return False
回答 12
data ="firstName and favoriteFood"
mandatory_fields =['firstName','lastName','age']# for eachfor field in mandatory_fields:if field notin data:print("Error, missing req field {0}".format(field));# still fine, multiple if statementsif('firstName'notin data or'lastName'notin data or'age'notin data):print("Error, missing a req field");# not very readable, list comprehension
missing_fields =[x for x in mandatory_fields if x notin data]if(len(missing_fields)>0):print("Error, missing fields {0}".format(", ".join(missing_fields)));
data = "firstName and favoriteFood"
mandatory_fields = ['firstName', 'lastName', 'age']
# for each
for field in mandatory_fields:
if field not in data:
print("Error, missing req field {0}".format(field));
# still fine, multiple if statements
if ('firstName' not in data or
'lastName' not in data or
'age' not in data):
print("Error, missing a req field");
# not very readable, list comprehension
missing_fields = [x for x in mandatory_fields if x not in data]
if (len(missing_fields)>0):
print("Error, missing fields {0}".format(", ".join(missing_fields)));