PythonÖÐRangeºÍXRangeµÄÇø±ð(Difference between Range and XRange in Python)
×î½ü»úÆ÷³öÁ˵ãÎÊÌ⣬ËùÒÔһֱûÓÐдеĶ«Î÷³öÀ´¡£Ö®Ç°ÔÚ¶ÁPythonµÄ´úÂëµÄʱºò£¬·¢¾õºÃ¶àÈËϲ»¶ÓÃXRange£¬¶ø²»ÊÇRange£¬ÎÒÒ²Ö»ÊǼǵÃѧϰµÄʱºò¿ªÊ¼Ñ§µ½µÄÊÇRange£¬ºóÃæÓÖ¿´µ½ÓиöXRange£¬µ±Ê±Ò²Ã»ÓÐÉ£¬ÎªÊ²Ã´PythonÀïÃæÒªÓÐÁ½¸öͬÑùµÄ¹¦ÄܵÄϵͳº¯Êý¡£½ñÌìÔÙÈ¥×Ðϸ²éÁËÏÂÎĵµ£¬ÔÀ´ËüÃÇÖ®¼ä»¹ÊÇÓеãÇø±ð£¬ËäÈ»²»ÊǺܴ󣬵«ÖÁÉÙXRangeµÄЧÂÊ»á±ÈRangeµÄ¸ß¡£
ÔÚÎĵµÖÐÊÇÕâÑùдµÄ£ºxrange([start,] stop[, step])This function is very similar to range(), but returns an ``xrange object'' instead of a list. This is an opaque sequence type which yields the same values as the corresponding list, without actually storing them all simultaneously. The advantage of xrange() over range() is minimal (since xrange() still has to create the values when asked for them) except when a very large range is used on a memory-starved machine or when all of the range's elements are never used (such as when the loop is usually terminated with break).Note: xrange() is intended to be simple and fast. Implementations may impose restrictions to achieve this. The C implementation of Python restricts all arguments to native C longs ("short" Python integers), and also requires that the number of elements fit in a native C long.
ÔÚRangeµÄ·½·¨ÖУ¬Ëü»áÉú³ÉÒ»¸ölistµÄ¶ÔÏ󣬵«ÊÇÔÚXRangeÖУ¬ËüÉú³ÉµÄÈ´ÊÇÒ»¸öxrangeµÄ¶ÔÏ󣬵±·µ»ØµÄ¶«Î÷²»ÊǺܴóµÄʱºò£¬»òÕßÔÚÒ»¸öÑ»·À»ù±¾É϶¼ÊÇ´ÓÍ·²éµ½µ×µÄÇé¿öÏ£¬ÕâÁ½¸ö·½·¨µÄЧÂʲ¶à¡£µ«ÊÇ£¬µ±·µ»ØµÄ¶«Î÷ºÜ´ó£¬»òÕßÑ»·Öг£³£»á±»Break³öÀ´µÄ»°£¬»¹Êǽ¨ÒéʹÓÃXRange£¬ÕâÑù¼ÈÊ¡¿Õ¼ä£¬ÓÖ»áÌá¸ßЧÂÊ¡£
ÏÂÃæ¾Ù¸öÀý×Ó£º
Èç¹ûʹÓÃrangeº¯Êý£¬Ö´ÐÐÏÂÃæµÄÓï¾ä£¬½«»áµÃµ½ºóÃæµÄ½á¹û£º
>>> a = range(0,100)
>>> print type(a)
>>> print a
[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,