python enumerateÓ÷¨
python cookbook
Recipe 2.5. Counting Lines in a File
£¬
½ñÈÕ·¢ÏÖÒ»¸öк¯Êý
enumerate
¡£Ò»°ãÇé¿ö϶ÔÒ»¸öÁбí»òÊý×é¼ÈÒª±éÀúË÷ÒýÓÖÒª±éÀúÔªËØÊ±£¬»áÕâÑùд£º
for
i
in
range
(0
,
len
(list
)):
print
i
,
list
[
i
]
µ«ÊÇÕâÖÖ·½·¨ÓÐЩÀÛ׸£¬Ê¹ÓÃ
ÄÚÖÃenumerrateº¯Êý»áÓиü¼ÓÖ±½Ó£¬ÓÅÃÀµÄ×ö·¨£¬ÏÈ¿´¿´enumerateµÄ¶¨Ò壺
def
enumerate
(collection
):
'Generates an indexed series:
(0,coll[0]), (1,coll[1]) ...'
i
=
0
it
=
iter
(collection
)
while
1
:
yield
(i
,
it
.
next
())
i
+=
1
enumerate»á½«Êý×é»òÁбí×é³ÉÒ»¸öË÷ÒýÐòÁС£Ê¹ÎÒÃÇÔÙ»ñÈ¡Ë÷ÒýºÍË÷ÒýÄÚÈݵÄʱºò¸ü¼Ó·½±ãÈçÏ£º
for
index
£¬
text
in
enumerate
(list
)):
print
index
,
text
ÔÚ
cookbookÀï½éÉÜ£¬Èç¹ûÄãÒª¼ÆËãÎļþµÄÐÐÊý£¬¿ÉÒÔÕâÑùд£º
count
=
len
(open
(thefilepath
,
‘
rU
’
).
readlines
())
Ç°ÃæÕâÖÖ·½·¨¼òµ¥£¬µ«ÊÇ¿ÉÄܱÈ
½ÏÂý£¬µ±Îļþ±È½Ï´óʱÉõÖÁ²»Äܹ¤×÷£¬ÏÂÃæÕâÖÖÑ»·¶ÁÈ¡µÄ·½·¨¸üºÏÊÊЩ¡£
Count
=
-
1
For
count
,
line
in
enumerate
(open
(thefilepath
,
‘
rU
’
))£º
Pass
Count
+=
1
Ïà¹ØÎĵµ£º
>>> import time
>>> import datetime
>>>
now = time.localtime()
>>> now
(2006, 4, 30, 18, 7, 35,
6, 120, 0)
>>> type(now)
<type 'time.struct_time'>
>>>
str_now = time.strftime("%m/%d/%Y %X", now )
>>>
str_n ......
ºÜ³¤µÄÒ»¶Î´úÂ룬µ«ºÜÇå³þ¡£¹þ¹þ¡£
import os
from time import strftime
stamp=strftime("%Y-%m-%d %H:%M:%S")
logfile = 'F:\\test\\m-php-framework\\tmp\logs\\error_report.log'
path = 'F:\\test\\'
files = os.listdir(path)
bytes = 0
numfiles = 0
for f in files:
if f.startswith('t'): ......
ÎÊÌâÃèÊö£º¼ÙÉèÎÒÓÐÕâÑùÁ½¸ölist£¬
Ò»¸öÊÇlist1£¬list1 = [1, 2, 3, 4, 5]
Ò»¸öÊÇlist2£¬list2 = [1, 4, 5]
ÎÒÃÇÈçºÎµÃµ½Ò»¸öеÄlist£ ......
[Ô´´]Python´úÂëÄ£¿éÈȸüлúÖÆÊµÏÖ(reload)
by AKara 2009-05-17 @ http://blog.csdn.net/akara @ akaras@163.com
---------------------------------------------------------------------
¶ÔÒ»¸öÓÎÏ·À´Ëµ£¬ÎÞÂÛÊÇclient»òserver¶¼·Ç³£ÐèÒªÒ»Ì×´úÂëÈȸüеĻúÖÆ¡£
ËüÄÜ´ó´óÌá¸ß¿ª·¢Ð§ÂÊ£¬ÓÖÄܳ¬ºõÍæ ......