python hidden features
from http://stackoverflow.com/questions/101268/hidden-features-of-python
http://www.okpython.com/bbs/thread-3434-1-1.html
http://www.okpython.com/bbs/thread-3436-1-2.html
http://www.okpython.com/bbs/thread-3437-1-2.html
http://www.okpython.com/bbs/thread-3438-1-1.html
http://www.okpython.com/bbs/thread-3439-1-1.html
http://www.okpython.com/bbs/thread-3440-1-1.html
http://www.okpython.com/bbs/thread-3441-1-1.html
#74 Ì«ÀÁ¶ø²»Ô¸¶Ô×ֵ佸Ðгõʼ»¯£¿Ã»ÎÊÌ⣡
# n Python > 2.3:
from collections import defaultdict
In Python <= 2.3:
def defaultdict(type_):
class Dict(dict):
def __getitem__(self, key):
return self.setdefault(key, type_())
return Dict()
In any version:
d = defaultdict(list)
for stuff in lots_of_stuff:
d[stuff.name].append(stuff)
Ïà¹ØÎĵµ£º
Python×Ö·û´®²Ù×÷
pythonÈçºÎÅжÏÒ»¸ö×Ö·û´®Ö»°üº¬Êý×Ö×Ö·û
python ×Ö·û´®±È½Ï
ÏÂÃæÁгöÁ˳£ÓõÄpythonʵÏÖµÄ×Ö·û´®²Ù×÷
1.¸´ÖÆ×Ö·û´®
#strcpy(sStr1,sStr2)
sStr1 = 'strcpy'
sStr2 = sStr1
sStr1 = 'strcpy2'
print sStr2
2.Á¬½Ó×Ö·û´®
#strcat(sStr1,sStr2)
sStr1 = 'strcat'
sStr2 = 'appen ......
ÔËÐÐÒ»¾äpythonÃüÁî
¶ÔvcÉèÖ÷¾¶
include:D:\PYTHON31\INCLUDE
lib:D:\PYTHON31\LIBS
#include "stdafx.h"
#include "python.h"
int main(int argc, char* argv[])
{
Py_Initialize() ;
PyRun_SimpleString("print('Hello')");
//PyRun_SimpleString("print(dir())");
Py_Finalize();& ......
ÕÒÁ˰ëÌìûÕÒ×Å£¬ÖÕÓÚÔÚÓ¢ÎÄÕ¾µãÉÏÕÒµ½£¬»¹ÓиÐлȺÀïµÄʯͷºÍÇòÃÔ
>>> s = datetime.datetime(2009,1,1)
>>> time.mktime(s.timetuple())
1230739200.0
±ðÍ⸶һ¸öpython¶Ôʱ¼äµÄһЩº¯Êý£¬ºÜºÃÓõÄ
ÎÒÃÇÏȵ¼Èë±ØÐëÓõ½µÄÒ»¸ömodule
>>> import time
ÉèÖÃÒ»¸öʱ¼äµÄ¸ñʽ£¬ÏÂÃæ»áÓõ½
& ......
from http://blog.alexa-pro.cn/?p=189
1: os.listdir(path) //pathΪĿ¼
¹¦ÄÜÏ൱ÓÚÔÚpathĿ¼ÏÂÖ´ÐÐdirÃüÁ·µ»ØÎªlistÀàÐÍ
print os.listdir('..')
Êä³ö£º
[a,b,c,d]
2: os.path.walk(path,visit,arg)
path £ºÊǽ«Òª±éÀúµÄĿ¼
visit £ºÊÇÒ»¸öº¯ÊýÖ¸Õ룬º¯ÊýÔ²ÐÎΪ£º
callback(arg,dir,fileList)
ÆäÖÐargΪΠ......