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)
Ïà¹ØÎĵµ£º
½ñÌìÒ»ÉϿΣ¬´óÅ£ÀÏʦ¾Í¸ø´ó¼Ò³öÁËÒ»µÀÌ⣺
±à³Ì£ºÇë´Ó×Ö·û´®“goOoOogle”ÖÐÕÒ³öÒÔ“O”¿ªÍ·£¬²¢ÒÔ“O”½áÊøµÄ²¿·Ö¡£
“Õ⻹²»¼òµ¥£¬¿´ÎҵĔС²Ë²»Ò»»á¶ù¾Í¸ø³öÁ˴𰸣º
>>> s="goOoOogle"
>>> s.find("O")
2
>>> s.find("O",3)
4
>>& ......
ÕÒÁ˰ëÌìûÕÒ×Å£¬ÖÕÓÚÔÚÓ¢ÎÄÕ¾µãÉÏÕÒµ½£¬»¹ÓиÐлȺÀïµÄʯͷºÍÇòÃÔ
>>> s = datetime.datetime(2009,1,1)
>>> time.mktime(s.timetuple())
1230739200.0
±ðÍ⸶һ¸öpython¶Ôʱ¼äµÄһЩº¯Êý£¬ºÜºÃÓõÄ
ÎÒÃÇÏȵ¼Èë±ØÐëÓõ½µÄÒ»¸ömodule
>>> import time
ÉèÖÃÒ»¸öʱ¼äµÄ¸ñʽ£¬ÏÂÃæ»áÓõ½
& ......
×÷ÕߣºÀÏÍõ
PythonËÆºõºÜÌÖÑáÐÞÊηû£¬Ã»Óг£¼ûµÄstaticÓï·¨¡£Æä¾²Ì¬·½·¨µÄʵÏÖ´óÖÂÓÐÒÔÏÂÁ½ÖÖ·½·¨£º
µÚÒ»ÖÖ·½Ê½£¨staticmethod£©£º
>>> class Foo:
str = "I'm a static method."
def ba ......
"""A parser for HTML and XHTML."""
# This file is based on sgmllib.py, but the API is slightly different.
# XXX There should be a way to distinguish between PCDATA (parsed
# character data -- the normal case), RCDATA (replaceable character
# data -- only char and entity references and end tags a ......
from http://blog.alexa-pro.cn/?p=349
ÄÚÈݸÅÒª:Python´úÂë¼ÓÃÜ,Ô´Âë±£»¤,´úÂë±£»¤,pycÎļþ
------------------------------------------------
¹ØÓÚpython´úÂë±£»¤ÔÚÍøÉϺÃÏñһֱûÓкܺõĽéÉÜ,¿ÉÄܺÍPYTHONµÄÉè¼Æ³õÖÔ"¿ªÔ´"ÓйØ,µ«Ô½À´Ô½¶àµÄÐÖµÜдµÄ¼ÓÈëPYTHONÕóÓª,¿ª·¢¹¤¾ßÓÃ;¸÷Òì,ÔÚÉÌÒµÓ¦ÓÃÖдúÂë± ......