Reputation: 23
I need to delete the elements that are duplicated in a dictionary like this:
{
0: array([506, 162]),
1: array([507, 162]),
2: array([506, 163]),
3: array([506, 162]),
4: array([506, 162]),
5: array([507, 162]),
6: array([506, 162]),
7: array([507, 162]),
8: array([509, 163]),
9: array([509, 163]),
10: array([506, 162]),
11: array([507, 162]),
12: array([509, 163]),
13: array([509, 163]),
14: array([508, 163]),
15: array([509, 162]),
16: array([509, 162]),
17: array([506, 163]),
18: array([507, 162]),
19: array([509, 163]),
20: array([509, 163]),
21: array([509, 164]),
22: array([509, 162]),
23: array([510, 162]),
24: array([510, 163]),
25: array([511, 156]),
26: array([511, 161]),
27: array([543, 167]),
28: array([515, 161]),
29: array([515, 162]),
30: array([545, 165]),
31: array([506, 163]),
32: array([507, 162]),
33: array([509, 163]),
34: array([511, 162]),
35: array([510, 162]),
36: array([515, 162]),
37: array([546, 167]),
38: array([512, 162]),
39: array([546, 169]),
40: array([516, 164]),
41: array([516, 164]),
42: array([516, 163]),
43: array([517, 164]),
44: array([517, 163]),
45: array([227, 95]),
46: array([516, 163]),
47: array([517, 163]),
48: array([516, 164]),
}
I tried with this code:
for key, centroide in ctrObjetosID.items():
if centroide in centroidesResult.values():
continue
centroidesResult[key] = centroide
But I get an error
The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Thanks for your help
Upvotes: 2
Views: 59
Reputation: 768
Try to change your code to the following:
for key, centroide in ctrObjetosID.items():
if len(centroidesResult.values()) == 0: # this check is necessary to prevent DeprecationWarning and make it save for future use
centroidesResult[key] = centroide
continue
if ((centroide == np.asarray(list(centroideResult.values()))).sum(axis=1) == 2).any():
# the equality comparision generates an elementwise boolean numpy array.
# The sum along axis 1 of this will generate an integer value for every entry in centroideResults
# and if both centroide coordinates are matching the result in the appropriate row is 2
# The any() checks at last, if any matching coordinates are found (rows with sum 2 are present).
continue
centroidesResult[key] = centroide
Upvotes: 1