Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to count unique tuples in the list?

I have a list of lets say 300 lenght. Each element consists tuple. Every tuple has 2 arrays. How can I count the unique tuples? I have 300 tuples here and I want to know how many of them are unique. I’m trying with converting list to the set and checking its len but I have such error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-46-24c20fbb5e62> in <module>
----> 1 len(set(pair_list))

TypeError: unhashable type: 'list'

My list looks like this:

[
   (array([ 0.,  0.1,  0., -0.2, -0., -0.2, 0.,  0.1], [ 0.5,  0.2,  0.3, -0.1, -0.4, -0.2, 0.2,  0.1]), array([ 0.123,  0.14,  0.1, -0.22, -0.5, -0.2, 0.,  0.1])),
   ...,
   (array(...), array(...))
]

Edit: This is real sample of my data. Just one element of the list:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[array([[-0.63434589, -0.29900576,  1.58925953,  1.58925953, -1.25893308,
           0.76126064, -0.87056499,  0.31736156, -3.86900902, -1.        ,
          -0.23059131, -0.78751513,  0.510954  , -1.        , -0.30160512,
           1.        ,  5.8423382 ,  0.02629687,  0.02696755,  1.65819659,
           4.21574931, -1.        ,  0.        ,  1.        ,  0.        ,
           0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
         [-1.        , -1.        , -1.        , -1.        , -1.        ,
          -1.        , -1.        , -1.        , -1.        , -1.        ,
          -1.        , -1.        , -1.        , -1.        , -1.        ,
          -1.        , -1.        , -1.        , -1.        , -1.        ,
          -1.        , -1.        , -1.        , -1.        , -1.        ,
          -1.        , -1.        , -1.        , -1.        , -1.        ],
         [-1.        , -1.        , -1.        , -1.        , -1.        ,
          -1.        , -1.        , -1.        , -1.        , -1.        ,
          -1.        , -1.        , -1.        , -1.        , -1.        ,
          -1.        , -1.        , -1.        , -1.        , -1.        ,
          -1.        , -1.        , -1.        , -1.        , -1.        ,
          -1.        , -1.        , -1.        , -1.        , -1.        ]]),
  array([ 0.     ,  0.14983,  0.     , -0.26847, -0.     , -0.26847,
         -0.     ,  0.14983])],

>Solution :

You need to convert the lists to tuples, to be able to create a set out of them:

pairs_1 = [
    [1,2], [3,4], [5,6], [7,8]
]

print(len(set(tuple(p) for p in pairs_1)))

Out:

4
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading