原文地址:http://maxburstein.com/blog/python-shortcuts-for-the-python-beginner/

 

下面这些内容是我多年使用python总结出来一些有用的提示和工具,希望对读者们有些帮助。

变量交换

[python] view plaincopy

  1. x = 6  

  2. y = 5  

  3.     

  4. x, y = y, x  

  5.     

  6. print x  

  7. >>> 5  

  8. print y  

  9. >>> 6  


单行的if声明

[python] view plaincopy

  1. print "Hello" if True else "World"  

  2. >>> Hello  


字符串连接 最后一个是一个很酷的连接了两种不同类型的对象的方式。

[python] view plaincopy

  1. nfc = ["Packers""49ers"]  

  2. afc = ["Ravens""Patriots"]  

  3. print nfc + afc  

  4. >>> ['Packers''49ers''Ravens''Patriots']  

  5.     

  6. print str(1) + " world"  

  7. >>> 1 world  

  8.     

  9. print `1` + " world"  

  10. >>> 1 world  

  11.     

  12. print 1"world"  

  13. >>> 1 world  

  14. print nfc, 1  

  15. >>> ['Packers''49ers'1  


数字诀窍

[python] view plaincopy

  1. #Floor Division (rounds down)  

  2. print 5.0//2  

  3. >>> 2  

  4.     

  5. #2 raised to the 5th power  

  6. print 2**5  

  7. >> 32  

小心除法和浮点数!

[python] view plaincopy

  1. print .3/.1  

  2. >>> 2.9999999999999996  

  3.     

  4. print .3//.1  

  5. >>> 2.0  


数值比较

[python] view plaincopy

  1. x = 2  

  2.     

  3. if 3 > x > 1:  

  4.     print x  

  5. >>> 2  

  6.     

  7. if 1 < x > 0:  

  8.     print x  

  9. >>> 2  


同时遍历两个列表

[python] view plaincopy

  1. nfc = ["Packers""49ers"]  

  2. afc = ["Ravens""Patriots"]  

  3.     

  4. for teama, teamb in zip(nfc, afc):  

  5.     print teama + " vs. " + teamb  

  6.     

  7. >>> Packers vs. Ravens  

  8. >>> 49ers vs. Patriots  


遍历列表同时得出序号和内容

[python] view plaincopy

  1. teams = ["Packers""49ers""Ravens""Patriots"]  

  2. for index, team in enumerate(teams):  

  3.     print index, team  

  4.     

  5. >>> 0 Packers  

  6. >>> 1 49ers  

  7. >>> 2 Ravens  

  8. >>> 3 Patriots  


列表解析

[python] view plaincopy

  1. numbers = [1,2,3,4,5,6]  

  2. even = [number for number in numbers if number%2 == 0]  


字典解析

[python] view plaincopy

  1. teams = ["Packers""49ers""Ravens""Patriots"]  

  2. print {key: value for value, key in enumerate(teams)}  

  3. >>> {'49ers'1'Ravens'2'Patriots'3'Packers'0}  


初始列表值

[python] view plaincopy

  1. items = [0]*3  

  2. print items  

  3. >>> [0,0,0]  


列表转换成字符串

[python] view plaincopy

  1. teams = ["Packers""49ers""Ravens""Patriots"]  

  2. print ", ".join(teams)  

  3. >>> 'Packers, 49ers, Ravens, Patriots'  


从字典中获得值(key)

[python] view plaincopy

  1. data = {'user'1'name''Max''three'4}  

  2. is_admin = data.get('admin'False)  


列表的子集

[python] view plaincopy

  1. x = [1,2,3,4,5,6]  

  2.     

  3. #First 3   

  4. print x[:3]  

  5. >>> [1,2,3]  

  6.     

  7. #Middle 4  

  8. print x[1:5]  

  9. >>> [2,3,4,5]  

  10.     

  11. #Last 3  

  12. print x[-3:]  

  13. >>> [4,5,6]  

  14.     

  15. #Odd numbers  

  16. print x[::2]  

  17. >>> [1,3,5]  

  18.     

  19. #Even numbers  

  20. print x[1::2]  

  21. >>> [2,4,6]  


Collections模块 除了Python的内置数据类型,在collections模块中提供了一些额外的特殊用途。我发现计数器有时是非常有用的。你甚至可以找到一些有用的,如果你参与了今年Facebook的HackerCup。

[python] view plaincopy

  1. from collections import Counter  

  2.     

  3. print Counter("hello")  

  4. >>> Counter({'l'2'h'1'e'1'o'1})  


Itertools模块

[python] view plaincopy

  1. from itertools import combinations  

  2.     

  3. teams = ["Packers""49ers""Ravens""Patriots"]  

  4. for game in combinations(teams, 2):  

  5.     print game  

  6.     

  7. >>> ('Packers''49ers')  

  8. >>> ('Packers''Ravens')  

  9. >>> ('Packers''Patriots')  

  10. >>> ('49ers''Ravens')  

  11. >>> ('49ers''Patriots')  

  12. >>> ('Ravens''Patriots')  


False == True 这是一个有趣的一个比一个有用的技术。在Python中True和False是基本上是全局变量。因此:

[python] view plaincopy

  1. False = True  

  2. if False:  

  3.     print "Hello"  

  4. else:  

  5.     print "World"  

  6.     

  7. >>> Hello