求1到10中所有偶数的平方
1
2L = [i*i for i in range(1, 11) if i%2 == 0]
print(L)生成全排列
1
2L = [m+n for m in 'ABC' for n in 'XYZ']
print(L)列出当前目录下的所有文件和目录名
1
2
3import os
L = [d for d in os.listdir('.')]
print(L)把序列
[1, 3, 5, 7, 9]
变换成整数135791
2
3def f(x, y):
return x*10+y
print(reduce(f, [1,3,5,7,9]))求1到100的素数
1
2
3
4
5
6
7def prime(s):
for n in range(2,s):
if s % n == 0:
return False
return True
print filter(prime,range(2,101))