Python || How to find duplicates

Let’s start from ‘Counter’(https://docs.python.org/2/library/collections.html).

  • built-in container: ‘dict’, ‘list’, ‘set’ ‘tuple’
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    from collections import Counter

    test_src = ['a','b','a','b','c','b','a']
    result = Counter(test_src)
    elements = list(result.elements())
    most_common = result.most_common()

    print(result)
    print(elements)
    print(most_common)

    Counter({‘a’: 3, ‘b’: 3, ‘c’: 1})
    [‘a’, ‘a’, ‘a’, ‘b’, ‘b’, ‘b’, ‘c’]
    [(‘a’, 3), (‘b’, 3), (‘c’, 1)]

© 2022 Tiffany's Blog All Rights Reserved. 本站访客数人次 本站总访问量
Theme by hiero