新聞中心
以下是一些Python實用技巧和工具,希望能對大家學習有所幫助。

在漢南等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供做網(wǎng)站、成都網(wǎng)站設(shè)計 網(wǎng)站設(shè)計制作按需規(guī)劃網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計,網(wǎng)絡(luò)營銷推廣,外貿(mào)網(wǎng)站制作,漢南網(wǎng)站建設(shè)費用合理。
交換變量
x = 6 y = 5 x, y = y, x print x >>> 5 print y >>> 6
if 語句在行內(nèi)
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
數(shù)字技巧
#除后向下取整 print 5.0//2 >>> 2 # 2的5次方 print 2**5 >> 32
注意浮點數(shù)的除法
print .3/.1 >>> 2.9999999999999996 print .3//.1 >>> 2.0
數(shù)值比較
這是我見過諸多語言中很少有的如此棒的簡便法
x = 2 if 3 > x > 1: print x >>> 2 if 1 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]
字典推導
和列表推導類似,字典可以做同樣的工作:
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]
列表轉(zhuǎn)換為字符串
teams = ["Packers", "49ers", "Ravens", "Patriots"] print ", ".join(teams) >>> 'Packers, 49ers, Ravens, Patriots'
從字典中獲取元素
我承認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')
False == True比起實用技術(shù)來說這是一個很有趣的事,在python中,True和False是全局變量,因此:
False = True if False: print "Hello" else: print "World" >>> Hello
網(wǎng)站標題:創(chuàng)新互聯(lián)Python教程:給Python初學者的一些技巧
文章轉(zhuǎn)載:http://fisionsoft.com.cn/article/dhddpdh.html


咨詢
建站咨詢
