Is Python an ordered set?

Keywords: Lambda Python pip

Python has a An orderly dictionary . How about an orderly set?

#1 building

For many purposes, just calling sorted is enough. for example

>>> s = set([0, 1, 2, 99, 4, 40, 3, 20, 24, 100, 60])
>>> sorted(s)
[0, 1, 2, 3, 4, 20, 24, 40, 60, 99, 100]

If you want to reuse this feature, calling the sorted function incurs overhead, so you may want to save a list of results as long as you complete the operation to change the collection. If you need to maintain unique elements and sort them, I agree with the recommendation to use OrderedDict in a collection with any value, such as none.

#2 building

For this reason, there is a Orderly setup (probable New links )Formula, available from In Python 2 documentation Quote. Run on Py2.6 or later and 3.0 or later without modification. This interface is almost identical to a normal collection, except that initialization should be done using lists.

OrderedSet([1, 2, 3])

This is a MutableSet, so the signature of. union does not match the signature of. union, but because it contains \\\\\\\\\

def union(*sets):
    union = OrderedSet()
    return union

def union(self, *sets):
    for set in sets:
        self |= set

#3 building

Ordered set is a special case of ordered dictionary in function.

Dictionary keys are unique. Therefore, if a person ignores values in an ordered Dictionary (for example, by assigning them to None), then a person actually has an ordered set.

Starting with Python 3.1, Yes collections.OrderedDict . The following is an example implementation of OrderedSet. (note that only a few methods need to be defined or overridden: collections.OrderedDict and collections.MutableSet Can complete heavy work.)

import collections

class OrderedSet(collections.OrderedDict, collections.MutableSet):

    def update(self, *args, **kwargs):
        if kwargs:
            raise TypeError("update() takes no keyword arguments")

        for s in args:
            for e in s:

    def add(self, elem):
        self[elem] = None

    def discard(self, elem):
        self.pop(elem, None)

    def __le__(self, other):
        return all(e in other for e in self)

    def __lt__(self, other):
        return self <= other and self != other

    def __ge__(self, other):
        return all(e in self for e in other)

    def __gt__(self, other):
        return self >= other and self != other

    def __repr__(self):
        return 'OrderedSet([%s])' % (', '.join(map(repr, self.keys())))

    def __str__(self):
        return '{%s}' % (', '.join(map(repr, self.keys())))

    difference = property(lambda self: self.__sub__)
    difference_update = property(lambda self: self.__isub__)
    intersection = property(lambda self: self.__and__)
    intersection_update = property(lambda self: self.__iand__)
    issubset = property(lambda self: self.__le__)
    issuperset = property(lambda self: self.__ge__)
    symmetric_difference = property(lambda self: self.__xor__)
    symmetric_difference_update = property(lambda self: self.__ixor__)
    union = property(lambda self: self.__or__)

#4 building

Implementation on PyPI

Although others have pointed out that there is no built-in implementation of insertion order reservation set in Python (however), I feel that there is a lack of an answer to this question, which points out that the PyPI What can be found on.

As far as I know, there are:

Both implementations are based on Raymond Hettinger submitted to ActiveState Of Formula It's also mentioned in other answers. I checked out and determined the following

Key differences:

  • Ordered set (version 1.1)
    • Advantage: O (1) is used to find by index (for example, my set [5])
    • Disadvantage: remove(item) is not implemented
  • oset (version 0.1.3)
    • Advantage: O (1) for remove(item)
    • Disadvantage: obviously, O (n) is used for searching by index

Both implementations have O (1) for add(item) and contain (item in my set contains (item).

Unfortunately, neither implementation has method based set operations, such as set1.union (set2) - > you must use operator based forms, such as set1 | set2 set1 | set2 instead. For a complete list of set operation methods and their operator based equivalents, see“ Set object Of Python document .

I used ordered sets for the first time until I used remove(item) for the first time, which caused my script to crash not implemented error. Since I've never used index based lookup so far, I switched to oset at the same time.

If you know about other implementations on PyPI, please let me know in the comments.

#5 building

If you use a sort set to maintain the sort order, consider using the sort set implementation in PyPI. To do this, sortedcontainers Module provides SortedSet . Some benefits: pure Python, fast C implementation, 100% unit test coverage, hours of stress testing.

It is easy to install from PyPI via pip:

pip install sortedcontainers

Please note that if you are unable to do pip install, you only need to Open source repository Drop down and files.

After the installation is complete, you can:

from sortedcontainers import SortedSet

The sortedcontainers module also maintains several alternative implementations performance comparison .

There is also a comment on asking Python for its bag data type SortedList Data type, which can be used to effectively implement bag.

Posted by ctoshack on Wed, 29 Jan 2020 22:53:59 -0800