-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
itertools.count step #49282
Comments
I'd like to request a 'step' argument be added for itertools.count. It ('range' and 'count' are very similar - 'count' could even be merged into |
Am not too excited about this one. As a heavy itertools user myself, Have you had any real world use cases where count(start, step) would |
Here's a couple of functions I use with count and step: def cf_e():
'''return: (iterator) the infinite continued fraction for e
e=[2; 1, 2, 1, 1, 4, 1, 1, 6, 1 , ... , 1, 2k, 1, ...]
'''
yield 2
for k in itertools.count(2, 2):
yield 1
yield k
yield 1
def prime_factors(n):
'''n: (int > 1)
return: (list) the sorted list of tuples (p,e) of prime factors of n
p is a prime factor, e is the number of times the factor is used
'''
ret = []
if n <= 1:
return ret
# factors: use known (small) primes, then possible primes (odd numbers)
for factor in itertools.chain([2,3,5,7,11], itertools.count(13, 2)):
if factor*factor > n:
if n != 1:
ret += [(n, 1)]
break
for e in itertools.count(0):
div, mod = divmod(n, factor)
if mod != 0:
break
n = div
if e >= 1:
ret += [(factor, e)]
return ret |
Nice. Now I know that import itertools
def count(offset=0,stride=1):
for i in itertools.count():
yield offset+i*stride
# this version probably performs faster
def count(offset=0,stride=1):
while True:
yield offset
offset += stride |
I already use the second version of the count function you give (without Haskell has a powerful and concise list notation, some of which Python |
Probably a better prime factor algorithm uses Sieve of E. to generate The other algorithm uses a custom generator anyway. Oh well, good luck, Dave. |
I've looked again at what it would take to update the C code and think Suggest you use "range(start, sys.maxsize, step)" or just roll your own |
Found a way to do this while keeping full speed on the default case. Also, fixed an error where the start argument was rounded to the nearest Also, improved the utility over its cousin, range() by allowing floating See r69522 |
A couple of comments:
Is this wise? From a numerical perspective, it seems to me that using for x in count(0.0, 0.1):
do_something_with_x where it seems quite likely that what's intended is the more robust: for i in count():
x = i/10.0
do_something_with_x Second (unrelated) comment: on my machine, list(count()) appears to |
Not too worried about either issue. For the first, it makes the code For the second, the non-interruptability issue is everpresent throughout |
That's pretty cool. :-)
Fair enough. |
More coolness: count(Fraction(2,3), Fraction(1,6)) |
I run my shells with low priority so I can sneak around and kill them. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: