Ò׽ؽØÍ¼Èí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö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 Ôª×é
Ôª×éºÍÁбíÊ®·ÖÀàËÆ£¬Ö»²»¹ýÔª×éºÍ×Ö·û´®Ò»ÑùÊÇ ²»¿É±äµÄ ¼´Äã²»ÄÜÐÞ¸ÄÔª×é¡£Ôª×éͨ¹ýÔ²À¨ºÅÖÐÓöººÅ·Ö¸îµÄÏîÄ¿¶¨Òå¡£Ôª×éͨ³£ÓÃÔÚʹÓï¾ä»òÓû§¶¨ÒåµÄº¯ÊýÄܹ»°²È«µØ²ÉÓÃÒ»×éÖµµÄʱºò£¬¼´±»Ê¹ÓõÄÔª×éµÄÖµ²»»á¸Ä±ä¡£
ʹÓÃÔª×é
Àý9.2 ʹÓÃÔª×é
#!/usr/bin/python
# Filename: using_tuple.py
zoo = ('wo ......

Python±àÂë¹æ·¶


±ê׼ͷ²¿
#!/usr/bin/python
±àÂë
ËùÓÐµÄ Python ½Å±¾Îļþ¶¼Ó¦ÔÚÎļþÍ·±êÉÏ #-*- coding:utf8 -*- ¡£ÉèÖñ༭Æ÷£¬Ä¬Èϱ£´æÎª utf8 ¸ñʽ¡£
mainº¯Êý
Ó¦ÓóÌÐòÒª¶¨Òåmainº¯Êý¶ø²»ÒªÖ±½Óд½Å±¾
def main():
dosomething()
if
__name__ == "__main__"
:
main()
Èç¹ûÊÇÈ«¾Ö±äÁ¿£¬ÔòÐèÒªÏÔʽ¼ÓÉ ......

PythonµÄlambdaº¯ÊýÓëÅÅÐò

ǰ¼¸Ìì¿´µ½ÁËÒ»ÐÐÇó1000µÄ½×³ËµÄPython´úÂ룺
print
  
reduce
(
lambda
  
x
,
y
:
x
*
y
,
  
range
(
1
,
  
1001
))

Ò»ÏÂ×Ó±»python´úÂëµÄ¾«¼ò
Óë½ô´ÕËùÕÛ·þ£¬¹Ê¶Ô´úÂë½øÐÐÁ˼òµ¥µÄ·ÖÎö¡£
reduceÓërange¶¼ÊÇPythonµÄÄÚÖú¯Êý¡£
range£¨1£¬10 ......

Python ´¦Àí×Ö½Ú


zz from: http://blog.sina.com.cn/s/blog_4b5039210100f1tu.html
Ô­ÎÄÓеãС´íÎ󣬸ÄÁËÒ»µãµã¡£
ÎÒÃÇÖªµÀpythonÖ»¶¨ÒåÁË6ÖÖÊý¾ÝÀàÐÍ£¬×Ö·û´®£¬ÕûÊý£¬¸¡µãÊý£¬ÁÐ±í£¬Ôª×飬×ֵ䡣µ«ÊÇCÓïÑÔÖÐÓÐЩ×Ö½ÚÐ͵ıäÁ¿£¬ÔÚpythonÖиÃÈçºÎʵÏÖÄØ£¿ÕâµãÆÄÎªÖØÒª£¬ÌرðÊÇÒªÔÚÍøÂçÉϽøÐÐÊý¾Ý´«ÊäµÄ»°¡£

pythonÌṩÁËÒ»¸östruc ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ