Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Condition in range function in python

Is there any syntax in python to create range with condition ?
For example:

in range(101, 9999999999999999999999999999999999999999999)

count all numbers, divisible by 11. Iterating over all range like

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

len([i for i in range(101, 9999999999999999999999999999999999999999999) if % 11 = 0]) is very slow. I tried to do vectorization filtering on numpy array

np.arange(101, 9999999999999999999999999999999999999999999, dtype=np.int8)

but numpy throws

Maximum allowed size exceeded

So my guess is to initially do not include do not dividable numbers in range. Is there any way to do it in range function ?

>Solution :

Any number that is divisible by 11 would be starting at the first number after your starting index that can be divided by 11 (which should be quick to find: s_idx + 11 - s_idx % 11 should work), and then using the step parameter to range to move 11 each time.

range(start, end, 11)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading