mardi 31 janvier 2017

remove duplicate tuple value based on index in tuple

Is there a way to remove a duplicate tuple based on the index in the tuple. Say I have

[(0, 4, 1.0), (1, 4, 1.0), (3, 4, 1.0), (0, 3, 2.0), (1, 3, 2.0), (0, 2, 3.0), (1, 2, 3.0), (2, 4, 4.0), (2, 3, 5.0), (0, 1, inf)]

Can I randomly keep one tuple where each duplicate has the same value at index 2?

So, there are 3 tuples that have value 1.0 at index 2, two tuples that have value 2.0 at index 2, one that has value 3 at index 2, and so on.

So, (0, 4, 1.0) might randomly be selected from the value 1.0 at index 2 and (1, 3, 2.0) might randomly be selected from the value 2.0 at index 2. Say, (1, 2, 3.0) was randomly selected from the value 3.0 at index 2. Then, my list would look like

[(0, 4, 1.0),(1, 3, 2.0), (1, 2, 3.0), (2, 4, 4.0), (2, 3, 5.0), (0, 1, inf)]

I have never come across a function that does this or at least efficiently.




Aucun commentaire:

Enregistrer un commentaire