Home > OS >  How to count unique tuples in the list?
How to count unique tuples in the list?

Time:11-05

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:

[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])],

CodePudding user response:

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

CodePudding user response:

This is standard python, any hashable item (of which a tuple is one) can be turned into a set. Then ask for it's length

len(set(list_of_tuples))

CodePudding user response:

you can try this

a = [[1, 2], [3, 4], [1, 2]]
c = list()
[c.append(x) for x in a if x not in c]
print(len(c))

resulting in

[[1, 2], [3, 4]]

len(c) will give you the unique length

CodePudding user response:

If is needed to do more complex analysis

# List of tuples preparation 
lst = [
        ([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]),
        ([0.123, 0.14, 0.1, -0.22, -0.5, -0.2, 0., 0.1], [0.123, 0.14, 0.1, -0.22, -0.5, -0.2, 0., 0.3]),
        ([0., 0.1, 0., -0.2, -0., -0.2, 0., 0.1], [0., 0.1, 0., -0.2, -0., -0.2, 0., 0.1]),
        ([0., 0.1, 0., -0.2, -0., -0.2, 0., 0.1], [0., 0.1, 0., -0.2, -0., -0.2, 0., 0.1]),
        ([0.123, 0.14, 0.1, -0.22, -0.5, -0.2, 0., 0.1], [0.123, 0.14, 0.1, -0.22, -0.5, -0.2, 0., 0.3])
]

# Calculate counts of each tuple
hashed_lst = {hash(frozenset(a1   a2)): (a1, a2) for a1, a2 in lst}
counter_lst = {k: 0 for k, _ in hashed_lst.items()}
for a1, a2 in lst:
    counter_lst[hash(frozenset(a1   a2))]  = 1

# Print all copies
for k, v in counter_lst.items():
    if v > 1:
        print(f"Tuple {hashed_lst[k]} has {v} copies")

# Print only unique tuples
for k, v in counter_lst.items():
    if v == 1:
        print(f"Tuple {hashed_lst[k]} is unique")
  • Related