Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- OrderedSet([1, 2, 3])
- @staticmethod
- def union(*sets):
- union = OrderedSet()
- union.union(*sets)
- return union
- def union(self, *sets):
- for set in sets:
- self |= set
- 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:
- self.add(e)
- 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__)
- >>> from boltons.setutils import IndexedSet
- >>> x = IndexedSet(list(range(4)) + list(range(8)))
- >>> x
- IndexedSet([0, 1, 2, 3, 4, 5, 6, 7])
- >>> x - set(range(2))
- IndexedSet([2, 3, 4, 5, 6, 7])
- >>> x[-1]
- 7
- >>> fcr = IndexedSet('freecreditreport.com')
- >>> ''.join(fcr[:fcr.index('.')])
- 'frecditpo'
- >>> keywords = ['foo', 'bar', 'bar', 'foo', 'baz', 'foo']
- >>> list(dict.fromkeys(keywords).keys())
- ['foo', 'bar', 'baz']
- pip install sortedcontainers
- from sortedcontainers import SortedSet
- help(SortedSet)
- >>> from collections_extended import setlist
- >>> sl = setlist('abracadabra')
- >>> sl
- setlist(('a', 'b', 'r', 'c', 'd'))
- >>> sl[3]
- 'c'
- >>> sl[-1]
- 'd'
- >>> 'r' in sl # testing for inclusion is fast
- True
- >>> sl.index('d') # so is finding the index of an element
- 4
- >>> sl.insert(1, 'd') # inserting an element already in raises a ValueError
- ValueError
- >>> sl.index('d')
- 4
- DataStructure = {
- 'Collections': {
- 'Map': [
- ('dict', 'OrderDict', 'defaultdict'),
- ('chainmap', 'types.MappingProxyType')
- ],
- 'Set': [('set', 'frozenset'), {'multiset': 'collection.Counter'}]
- },
- 'Sequence': {
- 'Basic': ['list', 'tuple', 'iterator']
- },
- 'Algorithm': {
- 'Priority': ['heapq', 'queue.PriorityQueue'],
- 'Queue': ['queue.Queue', 'multiprocessing.Queue'],
- 'Stack': ['collection.deque', 'queue.LifeQueue']
- },
- 'text_sequence': ['str', 'byte', 'bytearray']
- }
- >>> 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(not new_element in my_list):
- my_list.append(new_element)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement