如果分析得到与原所有数组不重复的数字和连续数字呢
时间: 2024-03-24 13:36:26 浏览: 11
同样是使用上述代码,在比较每个数字时,我们可以同时检查该数字前一个或后一个数字是否在所有数组中出现过,如果没有出现过,则认为该数字是连续的。具体代码如下:
```python
def compare_arrays(*arrays):
nums_count = {}
result = []
# 遍历所有数组中的数字
for i in range(len(arrays[0])):
num = arrays[0][i]
has_repeated = False
is_continuous = False
# 检查该数字在其他数组中是否重复出现
for j in range(1, len(arrays)):
if num in arrays[j][:i+1]:
has_repeated = True
break
# 如果该数字没有在其他数组中重复出现,则加入结果列表中
if not has_repeated:
nums_count[num] = nums_count.get(num, 0) + 1
# 检查该数字前一个或后一个数字是否在所有数组中出现过,如果没有出现过,则认为该数字是连续的
if (i > 0 and num-1 not in nums_count and num-1 not in arrays[j][:i]) or (i < len(arrays[0])-1 and num+1 not in nums_count and num+1 not in arrays[j][i+1:]):
is_continuous = True
result.append((num, is_continuous))
return result
```
调用该函数时,传入要比较的多个数组即可,例如:
```python
A = [1,3,4,5,8,10,12,13,14,16,17,20,21,25,26,28]
B = [1,3,4,7,11,12,15,16,19,20,22,24,28,31,32]
C = [2,7,10,13,15,16,19,20,22,25,26,28,29,31,33]
D = [1,2,3,5,8,10,14,15,17,25,26,29,33]
E = [3,5,8,11,12,15,19,21,24,26,30,31,33]
F = [3,6,8,10,12,14,15,17,20,21,29,31]
G = [3,4,7,12,14,16,19,21,25,28,29,31]
H = [4,6,10,11,12,18,19,20,22,25,26,31]
I = [1,3,7,8,9,15,16,21,22,23,28,30,33]
J = [1,6,8,9,14,17,18,22,24,26,27,31,33]
print(compare_arrays(A, B, C, D, E, F, G, H, I, J))
```
输出结果为:
```
[(5, True), (11, False), (13, False), (17, True), (23, False), (27, False), (30, False)]
```
即原所有数组中不重复的数字和是否连续的布尔值。