Questions tagged [xrange]

xrange is a Python function that, unlike the traditional 'range' function, creates an iterator object rather than a list.

xrange is a Python function that, unlike the traditional range function, creates an iterator object rather than a list.

Their behaviors are very similar, but behind the scenes it's different.

range returns a list:

>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(10).__class__
<type 'list'>
>>> for i in range(10):
...     print i,
0 1 2 3 4 5 6 7 8 9

xrange returns an iterable object:

>>> xrange(10)
xrange(10)
>>> xrange(10).__class__ #It's not a list, it's a class of its own.
<type 'xrange'>
>>> for i in xrange(10): #However, it can still be iterated over.
...     print i,
0 1 2 3 4 5 6 7 8 9

While they behave basically the same, xrange works differently. range generates a list and then returns it. xrange, typically used for low-memory systems or very large ranges, generates the numbers one at a time, once on each iteration rather than all at once before the loop.

Questions that include this tag should be related to xrange, not just code that happens to include it. Here are some relevant questions:

Relevant links:

91 questions
2
votes
3 answers

How to chunk up an interval in Python?

Is there a built-in/existing library function that is like xrange but splits an interval into even spaced non-overlapping chunks? For instance, if we call this function xchunks, then I would like: >>> xchunks(start=0, stop=18, step=5) [(0, 4), (5,…
mchen
  • 9,808
  • 17
  • 72
  • 125
2
votes
3 answers

xrange as an iterator and chunking

The snippets xi = xrange(10) zip(xi,xi) and xi = iter(range(10)) zip(xi,xi) behave differently. I expected to get [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)] in the first snippet as well, but it returns [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4),…
srean
  • 2,578
  • 18
  • 18
2
votes
4 answers

Number Sequence in MySQL

In Python if I wanted a sequence from 0 - 9 (inclusive) I would use xrange(0,10) . Is there a way I can do this in MySQL?
illuminatedtiger
  • 1,531
  • 3
  • 12
  • 8
1
vote
3 answers

random sample in python

I wanna speed this up: import random ndim = 50000 for i in xrange(ndim): random.sample([j for j in xrange(ndim) if j != i], 30000) I'm thinking about using NumPy but don't know how.
Academia
  • 3,984
  • 6
  • 32
  • 49
1
vote
1 answer

gnuplot - How do I change xrange when changing x-axis scale?º

I have the following data: 100 100.0 500 499.8 1000 999.0 2000 1994.4 3000 2918.8 10000 8233.8 99999 9433.2 I want to plot this so I have all x-axis labels equally spaced. Taking this link into account, I made the following script: set terminal png…
anmomu
  • 23
  • 4
1
vote
2 answers

Python3 and pypdf

I have a problem with pypdf and python3: when I do import pypdf or import PyPDF2 python shell returns an error "xrange is not define". I have seen this is a common problem and I tried the suggested workaround redefine xrange --> xrange = range add…
eugenio b
  • 19
  • 3
1
vote
2 answers

gnuplot - set xrange with date [1:31]

Hej! My automatized script is running through many month. I'd like to have the monthly plots to show always the window [1:31], actually YYYY-MM-01 to YYYY-MM-31. For all month my script is handling. (And I don't care that some month have no data at…
JaJo
  • 13
  • 3
1
vote
0 answers

Why the xrange function in the library code still works?

I'm running python code on the Google Colab with Python3 kernel. I found that in the scipy.spatial.distance.cdist source code (using ??), it uses xrange somewhere as for i in xrange(0, mA): for j in xrange(0, mB): …
1
vote
3 answers

How to set xrange on command line in gnuplot

I have several gnuplot scripts that draw graphs for me. I need to set the same xrange values ​​on the command line for everyone gnuplot scripts. I don't want to open each one separately. Code in gnuplot scripts(name:tlakD.gnuplot): set xdata time…
1
vote
1 answer

How to retrieval name data for a tooltip

I can not find how to retrieve the name of the data in the series to display it in the tooltip, with this.x it works but not this.series.data.name at the hover over the line, the name remains always 'undefined' I looked in the documentation but I…
1
vote
1 answer

Highcharts xrange refuses to exhibit drilldown behavior

Attempting to create a second level of data in the xrange charttype of highcharts does not behave as expected see jsfiddle https://jsfiddle.net/bo8eL42v/10/ and highcharts documentation…
Poncher
  • 53
  • 6
1
vote
2 answers

Difference between range() and xrange()

I am reading through a tutorial on Python and its says "There is a variant xrange() which avoids the cost of building the whole list for performance sensitive cases (in Python 3000, range() will have the good performance behavior and you can forget…
Ely Fialkoff
  • 642
  • 1
  • 5
  • 12
1
vote
2 answers

Add different colour in X-range bar based on percentage

I want to add different colours for parts of the bar based on partialfill. At the moment when the partialfill is 0.5 (50%) it makes half of the bar darker green. Is there a way to define colour in bar based on percentage? For example 0-49 make the…
Malvinator
  • 21
  • 3
1
vote
1 answer

GNUPlot xrange using dates in ruby

I have a csv file "harvest.csv", one of the columns contains dates. Here is what I came to (plot.rb): require 'csv' require 'gnuplot' days = Array.new mg = Array.new csv = CSV.open("../data/harvest.csv", headers: :first_row, converters:…
1
vote
2 answers

Is there a function like xrange in Lua?

Is there a function a function like xrange (of Python) in Lua, so I can do something like this: for i in xrange(10) do print(i) end It's different from that other question, because he is looking for a condition tester, but I'm not looking for a…
wb9688
  • 179
  • 10