【发布时间】:2020-10-04 19:48:23
【问题描述】:
我正在尝试根据取自另一个数组的位置在多维数组中查找重复值。上图正是我想要做的。
对于每个位置(或需要的行)从“位置数组”,检查“数字数组”中是否存在与第一个匹配的重复数字一个。
上图的例子应该返回(打印)类似:
- 重复次数:3,计数:3,行:0,0,0
- 重复次数:3,计数:3,行:0,1,2
并且应该忽略其他所有内容。
尝试了一千种不同的循环,但我失败了。
编辑:下面是一个无效的示例
print(finalValues)
for symbol in finalValues[0]:
count = 0
for line in lines:
for i in range(0, len(line)):
if finalValues[i][line[i]] == symbol:
count += 1
if count > 2:
print("Repeated number: {}, count: {}, line: {}".format(symbol, count, line))
编辑:数字示例(取自上图)
- We are looping through positions, and in the first loop we have positions: 1,1,1
- We should check numbers[0][1], numbers[1][1], numbers[2][1]
- In the next loop we have positions: 0,0,0
- We should check numbers[0][0], numbers[1][0], numbers[2][0]
- In the next loop we have positions: 2,2,2
- We should check numbers[0][2], numbers[1][2], numbers[2][2]
- In the next loop we have positions: 0,1,2
- We should check numbers[0][0], numbers[1][1], numbers[2][2]
【问题讨论】:
-
@usr2564301 添加了一个无效的示例,谢谢
-
位置数组到数字数组的映射是怎么做的?
-
@Anwarvic 位置是预定义的(硬编码),数字是在循环中随机生成的。它们是两个不同的数组,它们之间没有映射关系。
-
我尝试运行你的代码并得到
NameError: name 'finalValues' is not defined。请提供minimal reproducible example。 -
@Kevin ,finalValues 数组是图片上的绿色数字数组。只是命名不同。
标签: python arrays python-3.x loops for-loop