Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

PythonÖÐRangeºÍXRangeµÄÇø±ð

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,


Ïà¹ØÎĵµ£º

Python ×Ö·û´®


Python ×Ö·û´®
×Ö·û´®ÊÇ ×Ö·ûµÄÐòÁÐ ¡£×Ö·û´®»ù±¾ÉϾÍÊÇÒ»×éµ¥´Ê¡£
ÎÒ¼¸ºõ¿ÉÒÔ±£Ö¤ÄãÔÚÿ¸öPython³ÌÐòÖж¼ÒªÓõ½×Ö·û´®£¬ËùÒÔÇëÌرðÁôÐÄÏÂÃæÕⲿ·ÖµÄÄÚÈÝ¡£ÏÂÃæ¸æËßÄãÈçºÎÔÚPythonÖÐʹÓÃ×Ö·û´®¡£
ʹÓõ¥ÒýºÅ£¨'£©
Äã¿ÉÒÔÓõ¥ÒýºÅָʾ×Ö·û´®£¬¾ÍÈçͬ'Quote me on this'ÕâÑù¡£ËùÓеĿհף¬¼´¿Õ¸ñºÍÖƱí·û¶¼ÕÕÔ­Ñù±£Áô¡£ ......

´î½¨S60ÊÖ»ú¶ËPythonÈí¼þÔËÐÐ,¿ª·¢,·¢²¼Æ½Ì¨

µÚÒ»²½£º´î½¨ÊÖ»úÔËÐÐƽ̨
ÒªÔÚS60ÊÖ»úÉÏÔËÐÐPY¿ª·¢µÄÈí¼þ,Ê×ÏȾÍÒª´î½¨ÊÖ»úPYÔËÐл·¾³.ŵ»ùÑÇÊÖ»ú±¾Éí²»Ö§³ÖPython,ËùÒÔÒª°²×°Python²å¼þ.
PS£ºÒ»¶¨Òª°²×°ÔÚCÅÌ£¡ÎÒÒ»¿ªÊ¼×°E£¬½á¹û³öÏÖÁ˺ÜÑÏÖصÄÎÊÌ⣡²»¹ý,×î½üÍøÉÏ·¢ÏÖÓÐEÅÌ°æµÄÀ²,Äã¿ÉÒÔ×Ô¼ºÊÔÑéһϣ¬¹þ¹þ
PythonScriptShell_1_4_5_3rdEd.SIS
Pyth ......

Amusing Python 2: range/xrange

ÕâÁ½¸ö»ù±¾É϶¼ÊÇÔÚÑ­»·µÄʱºòÓá£
Python
´úÂë < type="application/x-shockwave-flash" width="14" height="15" src="http://cloudhe.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" src="http://cloudhe.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=for%20i% ......

Python Áбí(list)²Ù×÷

´´½¨Áбí
sample_list = ['a',1,('a','b')]
Python Áбí²Ù×÷
sample_list = ['a','b',0,1,3]
µÃµ½ÁбíÖеÄijһ¸öÖµ
value_start = sample_list[0]
end_value =
sample_list[-1]
ɾ³ýÁбíµÄµÚÒ»¸öÖµ
del sample_list[0]
ÔÚÁбíÖвåÈëÒ»¸öÖµ
sample_list[0:0] = ['sample value']
µÃµ½ÁбíµÄ³¤¶È
list_length = ......

python Êý×飬ÐòÁУ¬×ֵ䣬ÁÐ±í£¬Ðò±í£¬Ôª×棨tuple£©

python ûÓÐÊý×éµÄÊý¾Ý½á¹¹
#list ÐòÁÐ
sample_list = [1,2,3,'abc']
#dictionary ×Öµä
sample_dic = {"key":value, 2:3}
#tuple Ö»¶ÁµÄÐòÁÐ
sample_tuple = (1,3,"ab")
Ðò±í
    Ðò±íÓÉһϵÁÐÖµÓöººÅ·Ö¸ô¶ø³É
    Ðò±íÓë×Ö·û´®Ò»ÑùÊDz»¿É±äµÄ£º²»ÔÊÐí¶ÔÐ ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ