Python有序集吗?Python有一个有序字典..订好的一套怎么样?
3 回答
森林海
TA贡献2011条经验 获得超2个赞
OrderedSet([1, 2, 3])
.union__or__
@staticmethoddef union(*sets): union = OrderedSet() union.union(*sets) return uniondef union(self, *sets): for set in sets: self |= set
慕工程0101907
TA贡献1887条经验 获得超5个赞
有序集在功能上是有序字典的特例。
None
collections.OrderedDictcollections.OrderedDictcollections.MutableSet
import collectionsclass 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__)添加回答
举报
0/150
提交
取消
