In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I would like to talk to you about the similarities and differences between xrange and range in python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something from this article.
Source: http://ciniao.me/article.php?id=17
Range
Function description: range ([start,] stop [, step]), generates a sequence according to the range specified by start and stop and the step size set by step.
Range example:
> > range (5)
[0, 1, 2, 3, 4]
> range (1BI 5)
[1, 2, 3, 4]
> range (0pence6)
[0, 2, 4]
Xrange
Function description: the usage is exactly the same as range, except that what is generated is not an array, but a generator.
Xrange example:
> > xrange (5)
Xrange (5)
> > list (xrange (5))
[0, 1, 2, 3, 4]
> xrange (1BI 5)
Xrange (1,5)
> list (xrange (1penny 5))
[1, 2, 3, 4]
> xrange (0pence6)
Xrange (0,6,2)
> list (xrange (0mem6) 2)
[0, 2, 4]
As you can see from the above example: when you want to generate a large sequence of numbers, the performance of xrange is much better than that of range, because you don't need to open up a large piece of memory space in the first place, both of which are basically used in loops:
For i in range (0100):
Print i
For i in xrange (0100):
Print i
The results of both outputs are the same, but there are actually many differences. Range generates a list object directly:
A = range (0100)
Print type (a)
Print a
Print a [0], a [1]
Output result:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
0 1
Instead of generating a list directly, xrange returns one of the values for each call:
A = xrange (0100)
Print type (a)
Print a
Print a [0], a [1]
Output result:
Xrange (100)
0 1
So xrange loops perform better than range, especially when the return is large, try to use xrange, unless you are returning a list.
After reading the above, do you have any further understanding of the similarities and differences between xrange and range in python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.