標簽(空格分隔): Python 翻譯
來自 http://maxburstein.com/blog/python-shortcuts-for-the-python-beginner/
第一次試著翻譯二驰,不足之處,還請見量秉沼!
下面是我這些年在使用Python的過程中桶雀,收集的一些方法和工具,希望能幫助到讀者
交換變量
x = 6
y = 5
x, y = y, x
print x
>>> 5
print y
>>> 6
單行表達式
print "Hello" if True else "World"
>>> Hello
聯(lián)結(jié)
最后一種方式唬复,是聯(lián)結(jié)兩個不同類型對象的很好的方法
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ù)字技巧
#Floor Division (rounds down)
print 5.0//2
>>> 2
#2 raised to the 5th power
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 < x > 0:
print x
>>> 2
多列表同時循環(huán)
nfc = ["Packers", "49ers"]
afc = ["Ravens", "Patriots"]
for teama, teamb in zip(nfc, afc):
print teama + " vs. " + teamb
>>> Packers vs. Ravens
>>> 49ers vs. Patriots
加索引循環(huán)列表
teams = ["Packers", "49ers", "Ravens", "Patriots"]
for index, team in enumerate(teams):
print index, team
>>> 0 Packers
>>> 1 49ers
>>> 2 Ravens
>>> 3 Patriots
列表解析
第一種:
numbers = [1,2,3,4,5,6]
even = []
for number in numbers:
if number%2 == 0:
even.append(number)
或者
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語句太難看了休建,在字典對象中可以用一個小技巧乍恐,在字典取值時,如果沒有相應的項测砂,剛使用其他項來代替
一般寫法
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]
#First 3
print x[:3]
>>> [1,2,3]
#Middle 4
print x[1:5]
>>> [2,3,4,5]
#Last 3
print x[-3:]
>>> [4,5,6]
#Odd numbers
print x[::2]
>>> [1,3,5]
#Even numbers
print x[1::2]
>>> [2,4,6]
FizzBuzz in 60 Characters
以前,Jeff Atwood這個小伙砌些,提出了一個稱為“FizzBuzz”r 簡單程序練習呜投,這是它的解釋
Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".
要求:打印1到100,遇到3的倍數(shù)存璃,只打印“Fizz”,遇到5的倍數(shù)仑荐,打印“Buzz”,同時遇到3,5的倍數(shù)纵东,打印“FizzBuzz”
很簡單的解決辦法
for x in range(1,101):print"Fizz"[x%3*4:]+"Buzz"[x%5*4:]or x
集合
除了python的內(nèi)置數(shù)據(jù)類型粘招,在collections模塊中也有一些其他的特殊用法,我發(fā)現(xiàn)Counter很有用篮迎,
from collections import Counter
print Counter("hello")
>>> Counter({'l': 2, 'h': 1, 'e': 1, 'o': 1})
迭代工具 Itertools
除了上面說的collections男图,python還有一個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
這更多應該算是一個有意思的地方,而不是一個技巧岂傲,在python中True和False都是基本的公用變量难裆,因此:
False = True
if False:
print "Hello"
else:
print "World"
>>> Hello
謝謝閱讀。