Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. OrderedSet([1, 2, 3])
  2.  
  3. @staticmethod
  4. def union(*sets):
  5. union = OrderedSet()
  6. union.union(*sets)
  7. return union
  8.  
  9. def union(self, *sets):
  10. for set in sets:
  11. self |= set
  12.  
  13. import collections
  14.  
  15. class OrderedSet(collections.OrderedDict, collections.MutableSet):
  16.  
  17. def update(self, *args, **kwargs):
  18. if kwargs:
  19. raise TypeError("update() takes no keyword arguments")
  20.  
  21. for s in args:
  22. for e in s:
  23. self.add(e)
  24.  
  25. def add(self, elem):
  26. self[elem] = None
  27.  
  28. def discard(self, elem):
  29. self.pop(elem, None)
  30.  
  31. def __le__(self, other):
  32. return all(e in other for e in self)
  33.  
  34. def __lt__(self, other):
  35. return self <= other and self != other
  36.  
  37. def __ge__(self, other):
  38. return all(e in self for e in other)
  39.  
  40. def __gt__(self, other):
  41. return self >= other and self != other
  42.  
  43. def __repr__(self):
  44. return 'OrderedSet([%s])' % (', '.join(map(repr, self.keys())))
  45.  
  46. def __str__(self):
  47. return '{%s}' % (', '.join(map(repr, self.keys())))
  48.  
  49. difference = property(lambda self: self.__sub__)
  50. difference_update = property(lambda self: self.__isub__)
  51. intersection = property(lambda self: self.__and__)
  52. intersection_update = property(lambda self: self.__iand__)
  53. issubset = property(lambda self: self.__le__)
  54. issuperset = property(lambda self: self.__ge__)
  55. symmetric_difference = property(lambda self: self.__xor__)
  56. symmetric_difference_update = property(lambda self: self.__ixor__)
  57. union = property(lambda self: self.__or__)
  58.  
  59. >>> from boltons.setutils import IndexedSet
  60. >>> x = IndexedSet(list(range(4)) + list(range(8)))
  61. >>> x
  62. IndexedSet([0, 1, 2, 3, 4, 5, 6, 7])
  63. >>> x - set(range(2))
  64. IndexedSet([2, 3, 4, 5, 6, 7])
  65. >>> x[-1]
  66. 7
  67. >>> fcr = IndexedSet('freecreditreport.com')
  68. >>> ''.join(fcr[:fcr.index('.')])
  69. 'frecditpo'
  70.  
  71. >>> keywords = ['foo', 'bar', 'bar', 'foo', 'baz', 'foo']
  72.  
  73. >>> list(dict.fromkeys(keywords).keys())
  74. ['foo', 'bar', 'baz']
  75.  
  76. pip install sortedcontainers
  77.  
  78. from sortedcontainers import SortedSet
  79. help(SortedSet)
  80.  
  81. >>> from collections_extended import setlist
  82. >>> sl = setlist('abracadabra')
  83. >>> sl
  84. setlist(('a', 'b', 'r', 'c', 'd'))
  85. >>> sl[3]
  86. 'c'
  87. >>> sl[-1]
  88. 'd'
  89. >>> 'r' in sl # testing for inclusion is fast
  90. True
  91. >>> sl.index('d') # so is finding the index of an element
  92. 4
  93. >>> sl.insert(1, 'd') # inserting an element already in raises a ValueError
  94. ValueError
  95. >>> sl.index('d')
  96. 4
  97.  
  98. DataStructure = {
  99. 'Collections': {
  100. 'Map': [
  101. ('dict', 'OrderDict', 'defaultdict'),
  102. ('chainmap', 'types.MappingProxyType')
  103. ],
  104. 'Set': [('set', 'frozenset'), {'multiset': 'collection.Counter'}]
  105. },
  106. 'Sequence': {
  107. 'Basic': ['list', 'tuple', 'iterator']
  108. },
  109. 'Algorithm': {
  110. 'Priority': ['heapq', 'queue.PriorityQueue'],
  111. 'Queue': ['queue.Queue', 'multiprocessing.Queue'],
  112. 'Stack': ['collection.deque', 'queue.LifeQueue']
  113. },
  114. 'text_sequence': ['str', 'byte', 'bytearray']
  115. }
  116.  
  117. >>> s = set([0, 1, 2, 99, 4, 40, 3, 20, 24, 100, 60])
  118. >>> sorted(s)
  119. [0, 1, 2, 3, 4, 20, 24, 40, 60, 99, 100]
  120.  
  121. if(not new_element in my_list):
  122. my_list.append(new_element)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement