pep

Why is there no xrange function in Python3?

痞子三分冷 提交于 2019-11-26 06:54:34
问题 Recently I started using Python3 and it\'s lack of xrange hurts. Simple example: 1) Python2: from time import time as t def count(): st = t() [x for x in xrange(10000000) if x%4 == 0] et = t() print et-st count() 2) Python3: from time import time as t def xrange(x): return iter(range(x)) def count(): st = t() [x for x in xrange(10000000) if x%4 == 0] et = t() print (et-st) count() The results are, respectively: 1) 1.53888392448 2) 3.215819835662842 Why is that? I mean, why xrange\'s been

What's the function like sum() but for multiplication? product()?

懵懂的女人 提交于 2019-11-26 02:18:50
问题 Python\'s sum() function returns the sum of numbers in an iterable. sum([3,4,5]) == 3 + 4 + 5 == 12 I\'m looking for the function that returns the product instead. somelib.somefunc([3,4,5]) == 3 * 4 * 5 == 60 I\'m pretty sure such a function exists, but I can\'t find it. 回答1: Update: In Python 3.8, the prod function was added to the math module. See: math.prod(). Older info: Python 3.7 and prior The function you're looking for would be called prod() or product() but Python doesn't have that