依次同顺序对比取得与原所有数组不重复的数字,有相邻或相同的数字跳过
时间: 2024-03-18 15:41:51 浏览: 58
为了实现这个功能,我已经更新了上面的代码。现在代码将按顺序比较每个数组中仅出现一次的数字,如果相邻或相同的数字跳过,只添加第一个数字。以下是更新后的 Python 代码:
```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]
K = [1, 3, 4, 7, 12, 13, 15, 16, 18, 19, 20, 32, 33]
L = [1, 6, 10, 11, 12, 15, 17, 18, 19, 22, 32, 33]
M = [1, 7, 11, 13, 19, 20, 21, 22, 26, 27, 28, 32]
N = [2, 6, 8, 10, 13, 17, 18, 20, 24, 25, 27, 29]
O = [1, 2, 6, 10, 11, 12, 17, 18, 25, 28, 31, 33]
P = [4, 6, 7, 14, 20, 21, 22, 25, 29, 30, 31, 32]
R = [1, 2, 7, 9, 11, 14, 16, 26, 29, 30, 33]
S = [1, 3, 5, 7, 9, 15, 16, 21, 25, 28, 29]
T = [1, 2, 7, 9, 11, 14, 16, 26, 29, 30, 33]
# 将所有数组合并为一个列表
all_nums = A + B + C + D + E + F + G + H + I + J + K + L + M + N + O + P + R + S + T
# 定义一个用于存储不重复数字的列表
unique_nums = []
# 循环遍历所有数字,并将不重复的数字添加到列表中
for i in range(len(all_nums)):
# 如果当前数字已经添加到列表中,则跳过
if all_nums[i] in unique_nums:
continue
# 如果当前数字与前一个数字相同或相邻,则跳过
if i > 0 and (all_nums[i] == all_nums[i-1] or all_nums[i] == all_nums[i-1]+1):
continue
# 如果当前数字与后一个数字相邻,则跳过
if i < len(all_nums)-1 and all_nums[i] == all_nums[i+1]-1:
continue
# 如果当前数字在所有数组中仅出现一次,则添加到列表中
if all_nums.count(all_nums[i]) == 1:
unique_nums.append(all_nums[i])
# 输出不重复的数字
print(unique_nums)
```
运行这段代码会返回一个列表,其中包含所有不重复的数字,这些数字在所有数组中都不重复,并且没有相邻或相同的数字。
阅读全文