एफडब्ल्यूआईडब्ल्यू, यहां गैर-आरई विकल्प के एक जोड़े हैं जो मुझे लगता है कि पोक के समाधान की तुलना में शून्य हैं ।
इसके लिए पहला प्रयोग str.indexऔर जाँच ValueError:
def findall(sub, string):
"""
>>> text = "Allowed Hello Hollow"
>>> tuple(findall('ll', text))
(1, 10, 16)
"""
index = 0 - len(sub)
try:
while True:
index = string.index(sub, index + len(sub))
yield index
except ValueError:
pass
दूसरा परीक्षण का उपयोग करके str.findप्रहरी के लिए उपयोग करता है और जाँच करता है :-1iter
def findall_iter(sub, string):
"""
>>> text = "Allowed Hello Hollow"
>>> tuple(findall_iter('ll', text))
(1, 10, 16)
"""
def next_index(length):
index = 0 - length
while True:
index = string.find(sub, index + length)
yield index
return iter(next_index(len(sub)).next, -1)
इनमें से किसी भी कार्य को सूची में, टपल या अन्य चलने योग्य तार पर लागू करने के लिए , आप एक उच्च-स्तरीय फ़ंक्शन का उपयोग कर सकते हैं - जो किसी फ़ंक्शन को उसके तर्कों के रूप में लेता है - जैसे यह
def findall_each(findall, sub, strings):
"""
>>> texts = ("fail", "dolly the llama", "Hello", "Hollow", "not ok")
>>> list(findall_each(findall, 'll', texts))
[(), (2, 10), (2,), (2,), ()]
>>> texts = ("parallellized", "illegally", "dillydallying", "hillbillies")
>>> list(findall_each(findall_iter, 'll', texts))
[(4, 7), (1, 6), (2, 7), (2, 6)]
"""
return (tuple(findall(sub, string)) for string in strings)