每日python(5)

在python中相对一个列表的所有元素取组合,可以这么写:

1 ls = []
2 ls.append(1)
3 ls.append(2)
4 ls.append(3)
5 ls.append(4)
6 
7 for i in range(len(ls) - 1):
8     for j in range(i + 1, len(ls)):
9         print(ls[i], ls[j])

结果为:

(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)

  

原文地址:https://www.cnblogs.com/ivywenyuan/p/4776274.html