Python “set” with duplicate/repeated elements

后端 未结 8 1877
栀梦
栀梦 2020-12-28 12:52

Is there a standard way to represent a \"set\" that can contain duplicate elements.

As I understand it, a set has exactly one or zero of an element. I want functiona

8条回答
  •  臣服心动
    2020-12-28 13:15

    Your approach with dict with element/count seems ok to me. You probably need some more functionality. Have a look at collections.Counter.

    • O(1) test whether an element is present and current count retrieval (faster than with element in list and list.count(element))
    • counter.elements() looks like a list with all duplicates
    • easy manipulation union/difference with other Counters

提交回复
热议问题