Reputation: 2782
I am running a very lengthy script and this would work:
# sample line
line = '....,,,,.,..,.,.,.4GCCG.,..,,,.,.,,.2TG'
# search for numbers in line
numbers = re.search(["[0-9]", line)
if numbers is not None:
numbers = re.finditer("[0-9]", line)
some_process_on_each(numbers)
But searching twice for numbers in each line is not efficient.
Is there a way to compare the callable_iterator obtained from re.finditer("[0-9]", line)
to something to get a boolean (perhaps I can know the space on memory?) so I can do something like:
# sample line
line = '....,,,,.,..,.,.,.4GCCG.,..,,,.,.,,.2TG'
# search for numbers in line
numbers = re.finditer(["[0-9]", line)
if numbers is ??:
some_process_on_each(numbers)
Thank you
Upvotes: 0
Views: 189
Reputation: 43534
Just try iterating.
import re
line = '....,,,,.,..,.,.,.4GCCG.,..,,,.,.,,.2TG'
for n in re.finditer("[0-9]", line):
print(line[slice(*n.span())])
#4
#2
If there are no numbers, then there is nothing to iterate over.
Upvotes: 1