xrange

What is faster for loop using enumerate or for loop using xrange in Python?

ⅰ亾dé卋堺 提交于 2019-12-01 19:38:56
问题 What is faster, a for loop using enumerate or using xrange? EDIT: I have tested, and I just see minimal differences. 回答1: Enumerate is slightly faster. Tested in Python 3: >>>import pygame >>>pygame.init() >>>clock = pygame.time.Clock() >>>a = list(range(100000)) >>>def do_with_range(): ... clock.tick() ... k = 0 ... for i in range(len(a)): ... k += a[i] ... print(clock.tick()) >>>def do_with_enumerate(): ... clock.tick() ... k = 0 ... for i, j in enumerate(a): ... k += j ... print(clock.tick

range and xrange for 13-digit numbers in Python?

淺唱寂寞╮ 提交于 2019-11-27 23:38:54
range() and xrange() work for 10-digit-numbers. But how about 13-digit-numbers? I didn't find anything in the forum. You could try this. Same semantics as range: import operator def lrange(num1, num2 = None, step = 1): op = operator.__lt__ if num2 is None: num1, num2 = 0, num1 if num2 < num1: if step > 0: num1 = num2 op = operator.__gt__ elif step < 0: num1 = num2 while op(num1, num2): yield num1 num1 += step >>> list(lrange(138264128374162347812634134, 138264128374162347812634140)) [138264128374162347812634134L, 138264128374162347812634135L, 138264128374162347812634136L,

NameError: global name 'xrange' is not defined in Python 3

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 21:20:09
I am getting an error when running a python program: Traceback (most recent call last): File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 110, in <module> File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 27, in __init__ File "C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver\class\inventory.py", line 17, in __init__ builtins.NameError: global name 'xrange' is not defined The game is from here . What causes this error? You are trying to run a Python 2 codebase with Python 3. xrange() was renamed to range() in Python 3.

Why is there no xrange function in Python3?

早过忘川 提交于 2019-11-26 19:19:13
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 removed? It's such a great tool to learn. For the beginners, just like myself, like we all were at some point

`xrange(2**100)` -> OverflowError: long int too large to convert to int

安稳与你 提交于 2019-11-26 15:28:29
xrange function doesn't work for large integers: >>> N = 10**100 >>> xrange(N) Traceback (most recent call last): ... OverflowError: long int too large to convert to int >>> xrange(N, N+10) Traceback (most recent call last): ... OverflowError: long int too large to convert to int Python 3.x: >>> N = 10**100 >>> r = range(N) >>> r = range(N, N+10) >>> len(r) 10 Is there a backport of py3k builtin range() function for Python 2.x? Edit I'm looking for a complete implementation of "lazy" range() , not just a partial implementation of some of its functionality. Okay, here's a go at a fuller

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

Should you always favor xrange() over range()?

拟墨画扇 提交于 2019-11-26 03:15:56
问题 Why or why not? 回答1: For performance, especially when you're iterating over a large range, xrange() is usually better. However, there are still a few cases why you might prefer range() : In python 3, range() does what xrange() used to do and xrange() does not exist. If you want to write code that will run on both Python 2 and Python 3, you can't use xrange() . range() can actually be faster in some cases - eg. if iterating over the same sequence multiple times. xrange() has to reconstruct the

What is the difference between range and xrange functions in Python 2.X?

偶尔善良 提交于 2019-11-25 23:59:57
问题 Apparently xrange is faster but I have no idea why it\'s faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about for i in range(0, 20): for i in xrange(0, 20): 回答1: In Python 2.x: range creates a list, so if you do range(1, 10000000) it creates a list in memory with 9999999 elements. xrange is a sequence object that evaluates lazily. In Python 3, range does the equivalent of python's xrange , and to get the list, you have to use list(range(