计算列表中的重复列表

时间:2017-06-25 14:53:14

标签: python python-3.x count duplicates

lis = [[12,34,56],[45,78,334],[56,90,78],[12,34,56]]

我希望结果为2,因为重复列表的数量总共为2。我该怎么做?

我做过类似的事情

count=0
for i in range(0, len(lis)-1):
    for j in range(i+1, len(lis)):
        if lis[i] == lis[j]:
            count+=1

但计数值为1,因为它返回匹配的列表。如何获得重复列表的总数?

1 个答案:

答案 0 :(得分:3)

解决方案

如果您的子列表只包含数字,则可以使用collections.Counter,因此可以播放:

>>> from collections import Counter
>>> lis = [[12, 34, 56], [45, 78, 334], [56, 90, 78], [12, 34, 56]]
>>> sum(y for y in Counter(tuple(x) for x in lis).values() if y > 1)
2
>>> lis = [[12, 34, 56], [45, 78, 334], [56, 90, 78], [12, 34, 56], [56, 90, 78], [12, 34, 56]]
>>> sum(y for y in Counter(tuple(x) for x in lis).values() if y > 1)
5

步骤

将子列表转换为元组:

tuple(x) for x in lis

统计他们:

>>> Counter(tuple(x) for x in lis)
Counter({(12, 34, 56): 3, (45, 78, 334): 1, (56, 90, 78): 2})

仅采用以下值:

>>> Counter(tuple(x) for x in lis).values()
dict_values([3, 1, 2])

最后,只计算计数大于1的那些:

> sum(y for y in Counter(tuple(x) for x in lis).values() if y > 1)
5

使其可重复使用

将其放入函数,添加文档字符串和文档测试:

"""Count duplicates of sub-lists.
"""


from collections import Counter


def count_duplicates(lis):
    """Count duplicates of sub-lists.

    Assumption: Sub-list contain only hashable elements.
    Result: If a sub-list appreas twice the result is 2.
    If a sub-list aprears three time and a other twice the result is 5.

    >>> count_duplicates([[12, 34, 56], [45, 78, 334], [56, 90, 78],
    ...                   [12, 34, 56]])
    2
    >>> count_duplicates([[12, 34, 56], [45, 78, 334], [56, 90, 78],
    ...                   [12, 34, 56], [56, 90, 78], [12, 34, 56]])
    ...
    5
    """
    # Make it a bit more verbose than necessary for readability and
    # educational purposes.
    tuples = (tuple(elem) for elem in lis)
    counts = Counter(tuples).values()
    return sum(elem for elem in counts if elem > 1)


if __name__ == '__main__':

    import doctest

    doctest.testmod(verbose=True)

运行测试:

python count_dupes.py 
Trying:
    count_duplicates([[12, 34, 56], [45, 78, 334], [56, 90, 78],
                      [12, 34, 56]])
Expecting:
    2
ok
Trying:
    count_duplicates([[12, 34, 56], [45, 78, 334], [56, 90, 78],
                      [12, 34, 56], [56, 90, 78], [12, 34, 56]])
Expecting:
    5
ok
1 items had no tests:
    __main__
1 items passed all tests:
   2 tests in __main__.count_duplicates
2 tests in 2 items.
2 passed and 0 failed.
Test passed.