x = 6 y = 5 x, y = y, x print x >>> 5 print y >>> 6
print "Hello" if True else "World" >>> Hello
下面的最后一種方式在綁定兩個不同類型的對象時顯得很cool。
nfc = ["Packers", "49ers"] afc = ["Ravens", "Patriots"] print nfc + afc >>> ['Packers', '49ers', 'Ravens', 'Patriots'] print str(1) + " world" >>> 1 world print `1` + " world" >>> 1 world print 1, "world" >>> 1 world print nfc, 1 >>> ['Packers', '49ers'] 1
#除后向下取整 print 5.0//2 >>> 2 # 2的5次方 print 2**5 >> 32
print .3/.1 >>> 2.9999999999999996 print .3//.1 >>> 2.0
這是我見過諸多語言中很少有的如此棒的簡便法
x = 2 if 3 > x > 1: print x >>> 2 if 1 x > 0: print x >>> 2
nfc = ["Packers", "49ers"] afc = ["Ravens", "Patriots"] for teama, teamb in zip(nfc, afc): print teama + " vs. " + teamb >>> Packers vs. Ravens >>> 49ers vs. Patriots
teams = ["Packers", "49ers", "Ravens", "Patriots"] for index, team in enumerate(teams): print index, team >>> 0 Packers >>> 1 49ers >>> 2 Ravens >>> 3 Patriots
已知一個列表,我們可以刷選出偶數(shù)列表方法:
numbers = [1,2,3,4,5,6] even = [] for number in numbers: if number%2 == 0: even.append(number)
轉(zhuǎn)變成如下:
numbers = [1,2,3,4,5,6] even = [number for number in numbers if number%2 == 0]
是不是很牛呢,哈哈。
和列表推導(dǎo)類似,字典可以做同樣的工作:
teams = ["Packers", "49ers", "Ravens", "Patriots"] print {key: value for value, key in enumerate(teams)} >>> {'49ers': 1, 'Ravens': 2, 'Patriots': 3, 'Packers': 0}
items = [0]*3 print items >>> [0,0,0]
teams = ["Packers", "49ers", "Ravens", "Patriots"] print ", ".join(teams) >>> 'Packers, 49ers, Ravens, Patriots'
我承認(rèn)try/except代碼并不雅致,不過這里有一種簡單方法,嘗試在字典中查找key,如果沒有找到對應(yīng)的alue將用第二個參數(shù)設(shè)為其變量值。
data = {'user': 1, 'name': 'Max', 'three': 4} try: is_admin = data['admin'] except KeyError: is_admin = False data = {'user': 1, 'name': 'Max', 'three': 4} is_admin = data.get('admin', False)
有時,你只需要列表中的部分元素,這里是一些獲取列表子集的方法。
x = [1,2,3,4,5,6] #前3個 print x[:3] >>> [1,2,3] #中間4個 print x[1:5] >>> [2,3,4,5] #最后3個 print x[-3:] >>> [4,5,6] #奇數(shù)項 print x[::2] >>> [1,3,5] #偶數(shù)項 print x[1::2] >>> [2,4,6]
除了python內(nèi)置的數(shù)據(jù)類型外,在collection模塊同樣還包括一些特別的用例,在有些場合Counter非常實用。如果你參加過在這一年的Facebook HackerCup,你甚至也能找到他的實用之處。
from collections import Counter print Counter("hello") >>> Counter({'l': 2, 'h': 1, 'e': 1, 'o': 1})
和collections庫一樣,還有一個庫叫itertools,對某些問題真能高效地解決。其中一個用例是查找所有組合,他能告訴你在一個組中元素的所有不能的組合方式
from itertools import combinations teams = ["Packers", "49ers", "Ravens", "Patriots"] for game in combinations(teams, 2): print game >>> ('Packers', '49ers') >>> ('Packers', 'Ravens') >>> ('Packers', 'Patriots') >>> ('49ers', 'Ravens') >>> ('49ers', 'Patriots') >>> ('Ravens', 'Patriots')
比起實用技術(shù)來說這是一個很有趣的事,在python中,True和False是全局變量,因此:
False = True if False: print "Hello" else: print "World" >>> Hello
到此這篇關(guān)于新手必備的Python實用技巧和工具的文章就介紹到這了,更多相關(guān)Python實用技巧和工具內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:蘭州 六盤水 常州 江蘇 宿遷 山東 成都 駐馬店
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《新手必備的Python實用技巧和工具》,本文關(guān)鍵詞 新手,必備,的,Python,實用技巧,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。