Search for unique tuples

I want to find unique tuples based on the following situation:

This is my data:

data  = [
    (1L, u'ASN', u'Mon', u'15:15:00'), 
    (2L, u'ASN', u'Tue', u'15:15:00'), 
    (3L, u'ASN', u'Wed', u'15:15:00'), 
    (5L, u'ASN', u'Fri', u'15:15:00'), 
    (7L, u'ASN', u'Sun', u'15:15:00'), 
    (15L, u'ASN', 'Reminder', '2014-05-26 15:29'), 
    (16L, u'ASN', u'Mon', u'15:15:00'), 
    (17L, u'ASN', u'Tue', u'15:15:00'), 
    (18L, u'ASN', u'Wed', u'15:15:00'), 
    (19L, u'ASN', u'Fri', u'15:15:00')
]

I want to find unique tuples, ignoring the first element in the list, i.e.

(1L, u'ASN', u'Mon', u'15:15:00'), 
(2L, u'ASN', u'Mon', u'15:15:00') 

match up.

How can i do this?

+4
source share
1 answer

Put them in a dict with which you want to be unique.

uniq = {d[1:]:d for d in data}
uniq = uniq.values()

The above will return the last of any data items that are not unique. If you want first instead, you can change the original list.

If you need to use part of the data after searching for unique elements, you can easily replace it :dwith, for example. :d[0]in understanding dict.

+13
source

Source: https://habr.com/ru/post/1542277/


All Articles