1. <legend id='yD0SV'><style id='yD0SV'><dir id='yD0SV'><q id='yD0SV'></q></dir></style></legend>
          <bdo id='yD0SV'></bdo><ul id='yD0SV'></ul>

        <tfoot id='yD0SV'></tfoot>

        <small id='yD0SV'></small><noframes id='yD0SV'>

        <i id='yD0SV'><tr id='yD0SV'><dt id='yD0SV'><q id='yD0SV'><span id='yD0SV'><b id='yD0SV'><form id='yD0SV'><ins id='yD0SV'></ins><ul id='yD0SV'></ul><sub id='yD0SV'></sub></form><legend id='yD0SV'></legend><bdo id='yD0SV'><pre id='yD0SV'><center id='yD0SV'></center></pre></bdo></b><th id='yD0SV'></th></span></q></dt></tr></i><div id='yD0SV'><tfoot id='yD0SV'></tfoot><dl id='yD0SV'><fieldset id='yD0SV'></fieldset></dl></div>
      2. 带有 .index() 方法的 Python OrderedSet

        时间:2023-07-24
        <i id='CD7Me'><tr id='CD7Me'><dt id='CD7Me'><q id='CD7Me'><span id='CD7Me'><b id='CD7Me'><form id='CD7Me'><ins id='CD7Me'></ins><ul id='CD7Me'></ul><sub id='CD7Me'></sub></form><legend id='CD7Me'></legend><bdo id='CD7Me'><pre id='CD7Me'><center id='CD7Me'></center></pre></bdo></b><th id='CD7Me'></th></span></q></dt></tr></i><div id='CD7Me'><tfoot id='CD7Me'></tfoot><dl id='CD7Me'><fieldset id='CD7Me'></fieldset></dl></div>
            <bdo id='CD7Me'></bdo><ul id='CD7Me'></ul>
          • <legend id='CD7Me'><style id='CD7Me'><dir id='CD7Me'><q id='CD7Me'></q></dir></style></legend>
          • <small id='CD7Me'></small><noframes id='CD7Me'>

                  <tfoot id='CD7Me'></tfoot>
                    <tbody id='CD7Me'></tbody>
                  本文介绍了带有 .index() 方法的 Python OrderedSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有谁知道 python 的快速 OrderedSet 实现:

                  Does anyone know about a fast OrderedSet implementation for python that:

                  • 记住广告顺序
                  • 有一个 index() 方法(就像列表提供的那样)

                  我发现的所有实现都缺少 .index() 方法.

                  All implementations I found are missing the .index() method.

                  推荐答案

                  您可以随时将其添加到子类中.这是您在评论中链接的 OrderedSet 的基本实现:

                  You can always add it in a subclass. Here is a basic implementation for the OrderedSet you linked in a comment:

                  class IndexOrderedSet(OrderedSet):
                      def index(self, elem):
                          if key in self.map:
                              return next(i for i, e in enumerate(self) if e == elem)
                          else:
                              raise KeyError("That element isn't in the set")
                  

                  您提到您只需要 addindex 和按顺序迭代.您可以通过使用 OrderedDict 作为存储来获得它.作为奖励,您可以继承 collections.Set 抽象类以获得其他集合操作 frozenset 的支持:

                  You mentioned you only need add, index, and in-order iteration. You can get this by using an OrderedDict as storage. As a bonus, you can subclass the collections.Set abstract class to get the other set operations frozensets support:

                  from itertools import count, izip
                  from collections import OrderedDict, Set
                  
                  class IndexOrderedSet(Set):
                      """An OrderedFrozenSet-like object
                         Allows constant time 'index'ing
                         But doesn't allow you to remove elements"""
                      def __init__(self, iterable = ()):
                          self.num = count()
                          self.dict = OrderedDict(izip(iterable, self.num))
                      def add(self, elem):
                          if elem not in self:
                              self.dict[elem] = next(self.num)
                      def index(self, elem):
                          return self.dict[elem]
                      def __contains__(self, elem):
                          return elem in self.dict
                      def __len__(self):
                          return len(self.dict)
                      def __iter__(self):
                          return iter(self.dict)
                      def __repr__(self):
                          return 'IndexOrderedSet({})'.format(self.dict.keys())
                  

                  你不能继承 collections.MutableSet 因为你不能支持从集合中移除元素并保持索引正确.

                  You can't subclass collections.MutableSet because you can't support removing elements from the set and keep the indexes correct.

                  这篇关于带有 .index() 方法的 Python OrderedSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从字典值中创建一个集合 下一篇:pprint 排序字典但不是集合?

                  相关文章

                1. <i id='FWXIA'><tr id='FWXIA'><dt id='FWXIA'><q id='FWXIA'><span id='FWXIA'><b id='FWXIA'><form id='FWXIA'><ins id='FWXIA'></ins><ul id='FWXIA'></ul><sub id='FWXIA'></sub></form><legend id='FWXIA'></legend><bdo id='FWXIA'><pre id='FWXIA'><center id='FWXIA'></center></pre></bdo></b><th id='FWXIA'></th></span></q></dt></tr></i><div id='FWXIA'><tfoot id='FWXIA'></tfoot><dl id='FWXIA'><fieldset id='FWXIA'></fieldset></dl></div>
                  <tfoot id='FWXIA'></tfoot>

                    <bdo id='FWXIA'></bdo><ul id='FWXIA'></ul>

                  1. <legend id='FWXIA'><style id='FWXIA'><dir id='FWXIA'><q id='FWXIA'></q></dir></style></legend>
                    1. <small id='FWXIA'></small><noframes id='FWXIA'>