发生了一件非常奇怪和奇怪的事情。我试图在Compare 1 column of 2D array 中回答问题并删除重复的 Python,我做出了以下答案(我没有发布,因为该问题的一些现有答案非常紧凑和高效)但这是代码我做了:
array = [['abc',2,3,],
['abc',2,3],
['bb',5,5],
['bb',4,6],
['sa',3,5],
['tt',2,1]]
temp = []
temp2 = []
for item in array:
temp.append(item[0])
temp2 = list(set(temp))
x = 0
for item in temp2:
x = 0
for i in temp:
if item == i:
x+=1
if x >= 2:
while i in temp:
temp.remove(i)
for u in array:
for item in array:
if item[0] not in temp:
array.remove(item)
print(array)
代码应该可以工作,按照给定链接上的提问者的要求做。但我得到两对结果:
[['sa', 3, 5], ['tt', 2, 1]]
和
[['bb', 4, 6], ['tt', 2, 1]]
为什么相同的代码在相同的编译器上的同一操作系统上运行时会产生两个不同的答案?注意:结果不交替。我在上面列出的两个可能的输出之间是随机的。