首页 > 编程知识 正文

append python,python中set什么意思

时间:2023-05-05 02:18:59 阅读:156600 作者:1395

doesanyoneknowaboutafastorderedsetimplementationforpythonthat :

remembers内部订单

has an index () method (liketheonelistsoffer ) )。

allimplementationsifoundaremissingthe.index () method。

解决方案

youcanalwaysadditinasubclass.hereisabasicimplementationfortheorderedsetyoulinkedinacomment 3360

classindexorderedset (有序集) :

if key in self.map:

returnnext(IforI,einenumerate(self ) if e==elem ) ) ) ) ) ) ) ) ) ) )。

else:

raisekeyerror(thatelementisn ) tintheset ) )。

You mentioned you only need add,index,and in-order iteration.youcangetthisbyusinganordereddictasstorage.asa bonus, youcansubclassthecollections.setabstractclasstogettheothersetoperationsfrozensetssupport 3360

from itertools import count,izip

fromcollectionsimportordereddict,Set

类xorderedset (set ) :

“' ' An OrderedFrozenSet-like object

分配常数时间索引

but doesn ' tallowyoutoremoveelements ' ' '

self.num=count (

self.dict=有序dict (izip (iterable,self.num ) )

自助在if elem not in :

self.dict[elem]=next(self.num )

return self.dict[elem]

return elem in self.dict

returnlen(self.Dict ) ) ) ) ) ) )。

returniter(self.Dict ) ) ) ) ) ) )。

return'indexorderedset({} ).format (self.dict.keys ) )

you can ' tsubclasscollections.mutablesetbecauseyoucan ' tsupportremovingelementsfromthesetandkeeptheindexescorrect。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。