标签(空格分隔): 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
最后一种方式,是联结两个不同类型对象的很好的方法
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
#Floor Division (rounds down)
print 5.0//2
>>> 2
#2 raised to the 5th power
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
第一种:
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]
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]
以前,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的倍数,只打印“Fizz”,遇到5的倍数,打印“Buzz”,同时遇到3,5的倍数,打印“FizzBuzz”
很简单的解决办法
for x in range(1,101):print"Fizz"[x%3*4:]+"Buzz"[x%5*4:]or x
除了python的内置数据类型,在collections模块中也有一些其他的特殊用法,我发现Counter很有用,
from collections import Counter
print Counter("hello")
>>> Counter({'l': 2, 'h': 1, 'e': 1, 'o': 1})
除了上面说的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')
这更多应该算是一个有意思的地方,而不是一个技巧,在python中True和False都是基本的公用变量,因此:
False = True
if False:
print "Hello"
else:
print "World"
>>> Hello
谢谢阅读。
2024 - 快车库 - 我的知识库 重庆启连科技有限公司 渝ICP备16002641号-10
企客连连 表单助手 企服开发 榜单123