Hướng dẫn ordered set python
The answer is no, but you can use Show Update: As of Python 3.7 (and CPython 3.6), standard Here's an example of how to use
Asclepius 51.8k15 gold badges149 silver badges131 bronze badges answered Dec 6, 2018 at 18:21
8 There is an ordered set (possible new link) recipe for this which is referred to from the Python 2 Documentation. This runs on Py2.6 or later and 3.0 or later without any modifications. The interface is almost exactly the same as a normal set, except that initialisation should be done with a list.
This is a MutableSet, so the signature for
LondonRob 65.8k33 gold badges131 silver badges182 bronze badges answered Oct 31, 2009 at 10:15
CasebashCasebash 110k83 gold badges243 silver badges347 bronze badges 6 Update: This answer is obsolete as of Python 3.7. See jrc's answer above for a better solution. Will keep this answer here only for historical reasons. An ordered set is functionally a special case of an ordered dictionary. The keys of a dictionary are unique. Thus, if one disregards the values in an ordered dictionary (e.g. by assigning them As of Python 3.1 and
2.7 there is
answered Oct 31, 2009 at 10:17
Stephan202Stephan202 58.3k13 gold badges124 silver badges131 bronze badges 14 Implementations on PyPIWhile others have pointed out that there is no built-in implementation of an insertion-order preserving set in Python (yet), I am feeling that this question is missing an answer which states what there is to be found on PyPI. There are the packages:
Some of these implementations are based on the recipe posted by Raymond Hettinger to ActiveState which is also mentioned in other answers here. Some differences
Both implementations have O(1) for
answered Apr 22, 2014 at 16:22
Daniel KDaniel K 2,8792 gold badges23 silver badges23 bronze badges 3 I can do you one better than an OrderedSet: boltons has a pure-Python,
2/3-compatible Simply
Everything is unique and retained in order. Full disclosure: I wrote the
NOhs 2,7303 gold badges22 silver badges55 bronze badges answered Feb 7, 2016 at 20:41
2 If you're using the ordered set to maintain a sorted order, consider using a sorted set implementation from PyPI. The sortedcontainers module provides a SortedSet for just this purpose. Some benefits: pure-Python, fast-as-C implementations, 100% unit test coverage, hours of stress testing. Installing from PyPI is easy with pip:
Note that if you can't Once installed you can simply:
The sortedcontainers module also maintains a performance comparison with several alternative implementations. For the comment that asked about Python's bag data type, there's alternatively a SortedList data type which can be used to efficiently implement a bag. answered Sep 23, 2014 at 6:52
GrantJGrantJ 7,6263 gold badges49 silver badges45 bronze badges 6 As
other answers mention, as for python 3.7+, the dict is ordered by definition. Instead of subclassing
Then just:
I added this code, with some tests, in a small library, so anyone can just answered May 26, 2020 at 10:09
bustawinbustawin 6247 silver badges11 bronze badges 2 In case you're already using pandas in your code, its Examples from the article:
answered Sep 25, 2015 at 14:13
Berislav LopacBerislav Lopac 15.8k6 gold badges68 silver badges78 bronze badges 3 There's no
fhdrsdg 9,8072 gold badges37 silver badges58 bronze badges answered Dec 6, 2017 at 10:50
AbstProcDoAbstProcDo 18.3k14 gold badges70 silver badges122 bronze badges 1
As others have said,
The caveats are immutability and having to build up the set like a dict, but it's simple and only uses built-ins. answered Sep 2, 2020 at 2:33
David EhrmannDavid Ehrmann 7,1661 gold badge28 silver badges37 bronze badges The ParallelRegression package provides a setList( ) ordered set class that is more method-complete than the options based on the ActiveState recipe. It supports all methods available for lists and most if not all methods available for sets. answered Jan 21, 2017 at 22:45
There is a pip library that does this:
Then you can use it:
answered Apr 4 at 20:04
Watchdog101Watchdog101 5904 silver badges17 bronze badges For many purposes simply calling sorted will suffice. For example
If you are going to use this repeatedly, there will be overhead incurred by calling the sorted function so you might want to save the resulting list, as long as you're done changing the set. If you need to maintain unique elements and sorted, I agree with the suggestion of using OrderedDict from collections with an arbitrary value such as None. answered Feb 20, 2013 at 22:52
hwrdhwrd 4154 silver badges7 bronze badges 1 So i also had a small list where i clearly had the possibility of introducing non-unique values. I searched for the existence of a unique list of some sort, but then realized that testing the existence of the element before adding it works just fine.
I don't know if there are caveats to this simple approach, but it solves my problem. answered Jul 16, 2018 at 2:40
Loïc N.Loïc N. 3133 silver badges16 bronze badges 1 Not the answer you're looking for? Browse other questions tagged python set or ask your own question. |