I wrote a script in python to tell which numbers in new were in the first 10 numbers in new. I know it looks more complicated than it has too and that has to do with what I’m trying to do with the script later. For now though I’m trying to figure out why it’s printing ‘each’ for EVERY number in the list ‘new’ and not just the ones before the tenth.
Here’s my code:
i = 10
new = ['A lot of numbers']
for each in re.findall(r'[0-9]+', new):
if any(each for x in (re.findall(r'[0-9]+', new)[0:i])):
print each
else:
pass
You need to refer to x somehow in your generator expression, or else you’re just checking
any([each, each, each, ....]), which will always evaluate to true if each evaluates to true (which it always will for your regular expression). I suspect you want something like this, which tests if any of the first i items is equal to each:if any(x==each for x in (re.findall(r'[0-9]+', new)[0:i])):