Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding unique tuples

Tags:

python

tuples

I want to find unique tuples based upon 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') 

are the same.

How can I do it?

like image 486
PythonEnthusiast Avatar asked Dec 12 '22 05:12

PythonEnthusiast


1 Answers

Put them in a dict, keyed by the parts 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 non-unique. If you want the first instead, you can reverse the initial list.

If you only need to use a part of the data after finding the unique items, you can easily replace :d by e.g. :d[0] in the dict comprehension.

like image 175
otus Avatar answered Dec 30 '22 02:12

otus