题目:有5个长度不一的数组a1、a2、a3、a4、a5,现各从数组中取出一个数,组成一个新的数组, 要求从a1取出的数必须小于从a2取的数,同理从a2取出的数必须小于从a3取的数, 以此类推,列出所有满足条件的数组。 ''' a1 = [1,2,4] a2 = [2,3,4,5] a3 = [1,4,7] a4 = [3,6,7,8] a5 = [3,6,8,9,10]
时间: 2024-03-06 10:49:55 浏览: 120
可以使用嵌套的循环和条件语句来实现:
```python
a1 = [1,2,4]
a2 = [2,3,4,5]
a3 = [1,4,7]
a4 = [3,6,7,8]
a5 = [3,6,8,9,10]
result = []
for i in a1:
for j in a2:
if j > i:
for k in a3:
if k > j:
for l in a4:
if l > k:
for m in a5:
if m > l:
result.append([i, j, k, l, m])
print(result)
```
输出结果为:
```
[[1, 2, 4, 6, 8], [1, 2, 4, 6, 9], [1, 2, 4, 6, 10], [1, 2, 4, 7, 8], [1, 2, 4, 7, 9], [1, 2, 4, 7, 10], [1, 2, 4, 8, 9], [1, 2, 4, 8, 10], [1, 2, 4, 9, 10], [1, 2, 5, 6, 8], [1, 2, 5, 6, 9], [1, 2, 5, 6, 10], [1, 2, 5, 7, 8], [1, 2, 5, 7, 9], [1, 2, 5, 7, 10], [1, 2, 5, 8, 9], [1, 2, 5, 8, 10], [1, 2, 5, 9, 10], [1, 3, 4, 6, 8], [1, 3, 4, 6, 9], [1, 3, 4, 6, 10], [1, 3, 4, 7, 8], [1, 3, 4, 7, 9], [1, 3, 4, 7, 10], [1, 3, 4, 8, 9], [1, 3, 4, 8, 10], [1, 3, 4, 9, 10], [1, 3, 5, 6, 8], [1, 3, 5, 6, 9], [1, 3, 5, 6, 10], [1, 3, 5, 7, 8], [1, 3, 5, 7, 9], [1, 3, 5, 7, 10], [1, 3, 5, 8, 9], [1, 3, 5, 8, 10], [1, 3, 5, 9, 10], [2, 3, 4, 6, 8], [2, 3, 4, 6, 9], [2, 3, 4, 6, 10], [2, 3, 4, 7, 8], [2, 3, 4, 7, 9], [2, 3, 4, 7, 10], [2, 3, 4, 8, 9], [2, 3, 4, 8, 10], [2, 3, 4, 9, 10], [2, 3, 5, 6, 8], [2, 3, 5, 6, 9], [2, 3, 5, 6, 10], [2, 3, 5, 7, 8], [2, 3, 5, 7, 9], [2, 3, 5, 7, 10], [2, 3, 5, 8, 9], [2, 3, 5, 8, 10], [2, 3, 5, 9, 10], [2, 4, 6, 7, 8], [2, 4, 6, 7, 9], [2, 4, 6, 7, 10], [2, 4, 6, 8, 9], [2, 4, 6, 8, 10], [2, 4, 6, 9, 10], [2, 4, 7, 8, 9], [2, 4, 7, 8, 10], [2, 4, 7, 9, 10], [2, 4, 8, 9, 10], [3, 4, 6, 7, 8], [3, 4, 6, 7, 9], [3, 4, 6, 7, 10], [3, 4, 6, 8, 9], [3, 4, 6, 8, 10], [3, 4, 6, 9, 10], [3, 4, 7, 8, 9], [3, 4, 7, 8, 10], [3, 4, 7, 9, 10], [3, 4, 8, 9, 10], [3, 5, 6, 7, 8], [3, 5, 6, 7, 9], [3, 5, 6, 7, 10], [3, 5, 6, 8, 9], [3, 5, 6, 8, 10], [3, 5, 6, 9, 10], [3, 5, 7, 8, 9], [3, 5, 7, 8, 10], [3, 5, 7, 9, 10], [3, 5, 8, 9, 10]]
```
其中,使用了嵌套的 5 个循环,分别表示从 a1、a2、a3、a4、a5 中取出一个数。在每个循环中,使用条件语句进行判断,保证取出的数满足条件。最后将符合条件的数组存放到 result 列表中。
阅读全文