繁体   English   中英

如何计算列表中的唯一元组?

[英]How to count unique tuples in the list?

我有一个可以说 300 长度的列表。 每个元素由元组组成。 每个元组有 2 个数组。 如何计算唯一的元组? 我这里有 300 个元组,我想知道其中有多少是唯一的。 我正在尝试将列表转换为set并检查其len但我有这样的错误:

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

TypeError: unhashable type: 'list'

我的清单是这样的:

[
   (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(...))
]

编辑:这是我的数据的真实样本。 列表中只有一个元素:

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

您需要将lists转换为tuples ,以便能够从中创建一set

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

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

出去:

4

这是标准的python,任何可散列的项目(其中一个元组是一个)都可以变成一个集合。 然后询问它的长度

len(set(list_of_tuples))

你可以试试这个

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

导致

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

len(c) 会给你唯一的长度

如果需要做更复杂的分析

# 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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM