python 小技巧

来自:http://discuss.acmcoder.com/topic/5c7358e1908e7bb424216313

1. if语句在行内

print('Hello' if True else 'World')

Hello

2. 数值比较

x = 2
if 3>x>1:
    print(x)

if 1<x>0:
    print(x)

2

2

3. 迭代工具

和collections库相似,还有一个库itertools,能高效解决一些问题。

itertools库能查找列表元素所有组合。

from itertools import combinations

teams = ['wangke', 'wangyan', 'wangying']
for name in combinations(teams, 2):
    print(name)

原文地址:https://www.cnblogs.com/keye/p/10457181.html