30个有关Python的小技巧
本文摘自 PHPchina.com
1. 拆箱
1 | 1, 2, 3 a, b, c = |
2. 拆箱变量交换
1 | 1, 2 a, b = |
3. 扩展拆箱(只兼容python3)
1 | 1, 2, 3, 4, 5] a, *b, c = [ |
4. 负数索引
1 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a = [ |
5. 切割列表
1 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a = [ |
6. 负数索引切割列表
1 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a = [ |
7.指定步长切割列表
1 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a = [ |
8. 负数步长切割列表
1 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a = [ |
9. 列表切割赋值
1 | 1, 2, 3, 4, 5] a = [ |
10. 命名列表切割方式
1 | 0, 1, 2, 3, 4, 5] a = [ |
11. 列表以及迭代器的压缩和解压缩
1 | 1, 2, 3] a = [ |
12. 列表相邻元素压缩器
1 | 1, 2, 3, 4, 5, 6] a = [ |
14. 用压缩器反转字典
1 | 'a': 1, 'b': 2, 'c': 3, 'd': 4} m = { |
23. 统计在可迭代器中最常出现的元素
1 | 1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 6, 7]) A = collections.Counter([ |
24. 两端都可操作的队列
1 | Q = collections.deque() |
25. 有最大长度的双端队列
1 | 3) last_three = collections.deque(maxlen= |
26. 可排序词典
1 | for x in range(10)) m = dict((str(x), x) |
27. 默认词典
1 | m = dict() |
28. 默认字典的简单树状表达
1 | import json |
29. 对象到唯一计数的映射
1 | import itertools, collections |
30. 最大和最小的几个列表元素
1 | 0, 100) for __ in xrange(100)] a = [random.randint( |
31. 两个列表的笛卡尔积
1 | for p in itertools.product([1, 2, 3], [4, 5]): |
32. 列表组合和列表元素替代组合
1 | for c in itertools.combinations([1, 2, 3, 4, 5], 3): |
33. 列表元素排列组合
1 | for p in itertools.permutations([1, 2, 3, 4]): |
34. 可链接迭代器
1 | 1, 2, 3, 4] a = [ |
35. 根据文件指定列类聚
import itertools
with open('contactlenses.csv', 'r') as infile:
data = [line.strip().split(',') for line in infile]
...
data = data[1:]
def print_data(rows):
print '\n'.join('\t'.join('{: <16}'.format(s) for s in row) for row in rows)
...
print_data(data)
young myope no reduced none
young myope no normal soft
young myope yes reduced none
young myope yes normal hard
young hypermetrope no reduced none
young hypermetrope no normal soft
young hypermetrope yes reduced none
young hypermetrope yes normal hard
pre-presbyopic myope no reduced none
pre-presbyopic myope no normal soft
pre-presbyopic myope yes reduced none
pre-presbyopic myope yes normal hard
pre-presbyopic hypermetrope no reduced none
pre-presbyopic hypermetrope no normal soft
pre-presbyopic hypermetrope yes reduced none
pre-presbyopic hypermetrope yes normal none
presbyopic myope no reduced none
presbyopic myope no normal none
presbyopic myope yes reduced none
presbyopic myope yes normal hard
presbyopic hypermetrope no reduced none
presbyopic hypermetrope no normal soft
presbyopic hypermetrope yes reduced none
presbyopic hypermetrope yes normal none
data.sort(key=lambda r: r[-1])
for value, group in itertools.groupby(data, lambda r: r[-1]):
print '-----------'
print 'Group: ' + value
print_data(group)
...
-----------
Group: hard
young myope yes normal hard
young hypermetrope yes normal hard
pre-presbyopic myope yes normal hard
presbyopic myope yes normal hard
-----------
Group: none
young myope no reduced none
young myope yes reduced none
young hypermetrope no reduced none
young hypermetrope yes reduced none
pre-presbyopic myope no reduced none
pre-presbyopic myope yes reduced none
pre-presbyopic hypermetrope no reduced none
pre-presbyopic hypermetrope yes reduced none
pre-presbyopic hypermetrope yes normal none
presbyopic myope no reduced none
presbyopic myope no normal none
presbyopic myope yes reduced none
presbyopic hypermetrope no reduced none
presbyopic hypermetrope yes reduced none
presbyopic hypermetrope yes normal none
-----------
Group: soft
young myope no normal soft
young hypermetrope no normal soft
pre-presbyopic myope no normal soft
pre-presbyopic hypermetrope no normal soft
presbyopic hypermetrope no normal soft