python×ÖµäÅÅÐò
python×ÖµäÅÅÐò
1¡¢
×¼±¸ÖªÊ¶£º
ÔÚpythonÀ×ÖµädictionaryÊÇÄÚÖõÄÊý¾ÝÀàÐÍ£¬ÊǸöÎÞÐòµÄ´æ´¢½á¹¹£¬Ã¿Ò»ÔªËØÊÇkey-value¶Ô£º
È磺dict = {‘username’£º‘password’£¬‘database’£º‘master’}£¬ÆäÖБusername’ºÍ‘database’ÊÇkey£¬¶ø‘password’ºÍ‘master’ÊÇvalue£¬¿ÉÒÔͨ¹ýd[
key
]»ñµÃ¶ÔÓ¦ÖµvalueµÄÒýÓ㬵«ÊDz»ÄÜͨ¹ývalueµÃµ½key¡£
¶ÔÓÚdictionnary£¬ÐèÖªµÀÒÔϼ¸µã×¢ÒâÊÂÏ
a¡¢
dictionary µÄ key ÊÇ´óСдÃô¸ÐµÄ
£»
b¡¢
Ò»¸ödictionaryÖв»ÄÜÓÐÖظ´µÄ key
£»
c¡¢
d
ictionary
ÊÇÎÞÐòµÄ£¬Ã»ÓÐÔªËØ˳ÐòµÄ¸ÅÄ
ËüÃÇÖ»ÊÇÐòżµÄ¼òµ¥ÅÅÁÐ
¡£
2¡¢
×ÖµäÅÅÐòʵÏÖ£º
²Î¼ûcookbook£¬
Recipe 5.1. Sorting a Dictionary
½²ÊöÁË×ÖµäÅÅÐòµÄ·½·¨£»
Ç°ÃæÒÑ˵Ã÷d
ictionary
±¾ÉíûÓÐ˳Ðò¸ÅÄµ«ÊÇ×ÜÊÇÔÚijЩʱºò£¬µ«ÊÇÎÒÃdz£³£ÐèÒª¶Ô×Öµä½øÐÐÅÅÐò£¬Ôõô×öÄØ£¿ÏÂÃæ¸æËßÄ㣺
·½·¨1£º
×î¼òµ¥µÄ·½·¨£¬
ÅÅÁÐÔªËØ£¨key/value¶Ô£©
£¬
È»ºóÌô³öÖµ
¡£
×ÖµäµÄ
items
·½·¨£¬»á·µ»ØÒ»¸öÔª×éµÄÁÐ±í£¬ÆäÖÐÿ¸öÔª×鶼°üº¬Ò»¶ÔÏîÄ¿——¼üÓë¶ÔÓ¦µÄÖµ
¡£´ËʱÅÅÐò¿ÉÒÔ
sort()·½·¨
¡£
def
sortedDictValues1(adict):
items = adict.items()
items.sort()
return
[value for
key, value in
items]
·½·¨2£º
ʹÓÃÅÅÁмü
£¨key£©
µÄ·½Ê½£¬Ìô³öÖµ£¬Ëٶȱȷ½·¨1¿ì¡£
×Öµä¶ÔÏóµÄkeys()·½·¨·µ»Ø×ÖµäÖÐËùÓмüÖµ×é³ÉµÄÁÐ±í£¬´ÎÐòÊÇËæ»úµÄ¡£ÐèÒªÅÅÐòʱֻҪ¶Ô·µ»ØµÄ¼üÖµÁбíʹÓÃsort()·½·¨
¡£
def
sortedDictValues1(adict):
keys = adict.keys()
keys.sort()
return
[adict[key] for
key in
keys]
·½·¨3£º
ͨ¹ýÓ³ÉäµÄ·½·¨È¥¸üÓÐЧµÄÖ´ÐÐ×îºóÒ»²½
def
sortedDictValues1(adict):
keys = adict.keys()
keys.sort()
return
map£¨
adict.get,keys£©
·½·¨4£º
¶Ô×ֵ䰴¼üÅÅÐò£¬ÓÃÔª×éÁбíµÄÐÎʽ·µ»Ø£¬Í¬Ê±Ê¹ÓÃlambdaº¯ÊýÀ´½øÐУ»
sorted
(iterable
[
, cmp
[
Ïà¹ØÎĵµ£º
Ç°¼¸Ì죬СÐí¸øÎÒÒ»·ÝJavaQQµÄÔ´´úÂ룬ÓÃvim´ò¿ªÒ»¿´£¬·¢ÏÖÀïÃæµÄÖÐÎĶ¼ÊÇÂÒÂë¡£²»ÓÃ˵£¬ÓÖÊǿɶñµÄ±àÂëÎÊÌ⣬ÔÚwindowϵÄÎı¾Îļþͨ³£Ê¹ÓÃGBK»òGB18030±àÂ룬¶øÔÚLinuxÏÂutf-8±àÂëÔò´óÐÐÆäµÀ¡£´ò¿ª——Áí´æΪ¿Ï¶¨²»ÊÇÉϲߣ¬ÉÏÍøÕÒ±àÂëÅúÁ¿×ª»»¹¤¾ßÒ²²»ÊÇÔÛÇÚÀÍÓ¸ҵijÌÐòÔ±µÄ×÷·ç¡£×ÔÒѶ¯ÊÖ ......
def test2():
32 db = util.DBUnit('mysql_ab') &nb ......
×î½ü£¬ÏëÔÚÎÒµÄYouMoney£¨http://code.google.com/p/youmoney/£©ÀïÃæÔö¼ÓÌáÈ¡Óû§²Ù×÷ϵͳ°æ±¾ÐÅÏ¢¡£±ÈÈçwindowsÓû§£¬¿ÉÄÜÒª·µ»ØWindows XP ,»òÕßWindows 2003, Æ»¹ûÓû§Ó¦¸Ã·µ»ØMac OS X 10.5.8¡£ÓÃÁ˺ܶà°ì·¨£¬°üÀ¨ÔÚmacϵͳÀïµ÷ÓÃϵͳÃüÁȡ»·¾³±äÁ¿£¬µÈµÈ¡£×îºóÎÞÒâ·¢ÏÖ£¬ÔÀ´pythonÀïÀïÃæÓиöpl ......
ʵÑé»·¾³£ºwindows xp + vim
Îļþ£ºtest.py¡£±àÂ룺ansi
ÎÒÃǵÄÄ¿±ê²Ù×÷test.pyÖб£´æµÄ·ÇÓ¢ÎÄ×Öĸ¡£
ÎļþÍ·µÄ#encoding=utf8/gbk£¬Õâ¸öÊÇÓÃÀ´ËµÃ÷Ô´ÎļþµÄÓ²Å̱àÂëÒÔ±ãpythonʶ±ð[4]¡£
----------------------------------------------
ÊäÈë:
x = 'ÖÐÎÄ'
Êä³ö: ±àÒëʧ°Ü
±àÒëʱÐèÒªÖªµÀ‘ÖÐÎÄ’µÄÓ²Å̱à ......
ÎÒÃǾ³£»á²ÉÓÃÉú²úÕß/Ïû·ÑÕß¹ØϵµÄÁ½¸öÏß³ÌÀ´´¦ÀíÒ»¸ö¹²Ïí»º³åÇøµÄÊý¾Ý¡£ÀýÈçÒ»¸öÉú²úÕßÏ߳̽ÓÊÜÓû§Êý¾Ý·ÅÈëÒ»¸ö¹²Ïí»º³åÇøÀµÈ´ýÒ»¸öÏû·ÑÕßÏß
³Ì¶ÔÊý¾ÝÈ¡³ö´¦Àí¡£µ«ÊÇÈç¹û»º³åÇøµÄ̫С¶øÉú²úÕߺÍÏû·ÑÕßÁ½¸öÒì²½Ï̵߳ÄËٶȲ»Í¬Ê±£¬ÈÝÒ׳öÏÖÒ»¸öÏ̵߳ȴýÁíÒ»¸öÇé¿ö¡£ÎªÁ˾¡¿ÉÄܵÄËõ¶Ì¹²Ïí×ÊÔ´²¢ÒÔÏàͬ
Ëٶȹ¤×÷µÄ¸÷Ïß³ ......