我有一个任务,我需要找到类似于第一列的列数。我有一些 MxN 大小的数组,其中数字在0到100的范围内。首先,我想尝试使用静态数组(没有伪随机数)来解决它,但我卡住了,无法找到解决方案。到目前为止,我有这个:
firstCol = []
temp = []
amount = 0
table = [[36, 36, 78, 36, 38, 41],
[65, 6, 23, 65, 49, 89],
[18, 70, 77, 18, 59, 0],
[53, 46, 80, 66, 10, 13],
[33, 93, 26, 57, 37, 23],
[83, 37, 39, 27, 53, 100],
[1, 11, 46, 96, 98, 93],
[54, 33, 90, 88, 83, 58]]
firstCol = [e1[0] for e1 in table]
theSame = [False] *10
n=1
temp = [e2[n] for e2 in table]
其余代码没有t work, so I didn't write it here. The idea is to compare values of
firstCol`和临时数组。 temp的值将改变每个循环。如果有更好的想法如何做到这一点我会很高兴看到它,谢谢提前=)
答案 0 :(得分:1)
如果我的声望得分足够高,我会发表评论并说“#34;这看起来有点像家庭作业。"然后,我会尝试为您提供有关如何前进的一些指导,就像我建议您研究如何遍历数组一样。
但由于我只能回答问题,因此我会为您提供完成目标的代码(我认为)。希望这会有所帮助:
# Lets create a vector/array for our results and initalize it to zero
counts=[0] * len(table)
# step through the two dimensional array...
for index,row in enumerate(table):
# The first column is index 0 of the row
firstValue = row[0]
# Now just compare the firstValue to the rest of the values in the row
for colValue in row[1:]:
if colValue == firstValue:
counts[index] = counts[index] + 1
print counts