0
发生了一件非常奇怪而奇怪的事情。我试图在Compare 1 column of 2D array and remove duplicates Python回答这个问题,我做了以下的答案(我没有张贴,因为现存的一些答案,这个问题是很多紧凑和高效的),但是这是我做的代码:来自相同代码的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]]
为什么同一个操作系统上同样的代码上同所有相同的编译器产生两个不同的答案时运行?注意:结果不会交替。在上面列出的两个可能的输出之间是随机的。
您正在迭代'temp'和'array',同时从中移除值。这是你想要的吗? – ForceBru
啊!谢谢@ForceBru。如果您发布答案,我可以标记为正确。 – Octo