|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
从五组数各取一个数组成一个从小到大的列表,并将所有可能组合找出来,代码如下:
- #从五组数各取一个数组成一个从小到大的列表,并将所有可能组合找出来
- #五组随机数
- listA = [4, 2, 7, 8, 3]
- listB = [1, 6, 2, 7, 5]
- listC = [7, 3, 1, 6, 0]
- listD = [5, 8, 9, 2, 1]
- listE = [6, 1, 4, 3, 9]
- listN = []#新列表
- listNs = []#新列表组合
- #遍历A,先添加后弹出
- for a in listA:
- listN.append(a)
- #遍历B,找出B中比a[0]大的数添加到bt
- bt = []
- for b in listB:
- if b > a:
- bt.append(b)
- #遍历bt,先添加后弹出
- for b in bt:
- listN.append(b)
- #遍历C,找出C中比bt[0]大的数添加到ct
- ct = []
- for c in listC:
- if c > b:
- ct.append(c)
- #遍历ct,先添加后弹出
- for c in ct:
- listN.append(c)
- #遍历D,找出比ct[0]大的数添加到dt
- dt = []
- for d in listD:
- if d > c:
- dt.append(d)
- #遍历dt,先添加后弹出
- for d in dt:
- listN.append(d)
- #遍历E,找出比dt[0]大的数添加到et
- et = []
- for e in listE:
- if e > d:
- et.append(e)
- #遍历et,先添加后弹出
- for e in et:
- #将et[0]添加到listN中
- listN.append(e)
- #打印listN
- print(listN)
- listNs.append(listN)
- #弹出listN[-1]
- listN.pop()
- listN.pop()
- listN.pop()
- listN.pop()
- listN.pop()
-
- print(listNs)
复制代码
运行后显示所有列表元素为空,如下:
- [4, 6, 7, 8, 9]
- [4, 5, 7, 8, 9]
- [4, 5, 6, 8, 9]
- [2, 6, 7, 8, 9]
- [2, 5, 7, 8, 9]
- [2, 5, 6, 8, 9]
- [3, 6, 7, 8, 9]
- [3, 5, 7, 8, 9]
- [3, 5, 6, 8, 9]
- [[], [], [], [], [], [], [], [], []]
复制代码
百思不得其解,请大神指教
append列表,使用的是地址,你最后把列表pop干净了,里面相关的就都是空,可以使用切片或者深拷贝
|
|