首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >遍历集合的类

遍历集合的类
EN

Stack Overflow用户
提问于 2016-10-28 10:39:23
回答 2查看 167关注 0票数 2
代码语言:javascript
复制
class Ordered:
    def __init__(self,aset):
        self.aset = aset
    def __iter__(self):
        for v in sorted(self.aset): # iterate over list of values returned by sorted
            yield v

该函数接受一个集合并返回一个列表

集合始终是

S= {1,2,4,8,16}

例如:

代码语言:javascript
复制
s = {1, 2, 4, 8, 16}
i = iter(Ordered(s))
print(next(i))
print(next(i))
s.remove(8)
print(next(i))
s.add(32)
print(next(i))
print(next(i))

it should prints 1 2 4 16 32

但是当我的函数

代码语言:javascript
复制
[next(i), next(i), s.remove(8), next(i), next(i), s.add(32), next(i)]

它应该会打印出来

代码语言:javascript
复制
[1, 2, None, 4, 16, None, 32]

但是,它输出的是:

代码语言:javascript
复制
[1, 2, None, 4, 8, None, 16]

有人能告诉我怎么修吗?谢谢

我发布了下面的错误,以帮助理解:

代码语言:javascript
复制
39 *Error: Failed [next(i), next(i), s.remove(8), next(i), next(i), s.add(32), next(i)] == [1, 2, None, 4, 16, None, 32]
      evaluated: [1, 2, None, 4, 8, None, 16] == [1, 2, None, 4, 16, None, 32]
42 *Error: [next(i), next(i), next(i), s.add(3), next(i), s.add(10), s.add(32), next(i), next(i), next(i)] raised exception; unevaluated: [1, 2, 4, None, 8, None, None, 10, 16, 32]
46 *Error: Failed [next(i), s.remove(2), s.remove(4), s.remove(8), next(i)] == [1, None, None, None, 16]
      evaluated: [1, None, None, None, 2] == [1, None, None, None, 16]
49 *Error: Failed [next(i), s.remove(2), next(i), s.remove(4), s.remove(8), next(i)] == [1, None, 4, None, None, 16]
      evaluated: [1, None, 2, None, None, 4] == [1, None, 4, None, None, 16]
EN

回答 2

Stack Overflow用户

发布于 2016-10-28 15:30:27

sorted()函数对参数进行排序并返回一个列表。修改输入集不会影响排序列表。

如果您希望迭代器反映对原始集的更改,迭代器将需要在每次迭代时检查集的状态并做出适当的响应。

票数 0
EN

Stack Overflow用户

发布于 2016-10-28 17:06:35

使用sorted时,您将从集合中创建一个排序列表。该列表与创建它的原始集合没有任何连接,并且不会反映对该集合的任何更改。在按排序的顺序迭代元素时,您必须自己跟踪更改。

跟踪删除的元素很简单:只需检查当前元素是否仍在原始集合中。跟踪新元素要复杂一些。您可以使用heapq模块并从集合创建一个heap,而不是一个排序列表,然后您可以在迭代堆的同时将新元素添加到堆中。要查找新元素,请创建原始集的副本,并在每次迭代中比较这两个元素。此外,根据您的测试用例,您将必须检查当前元素是否比前一个元素小,并在这种情况下跳过它。

代码语言:javascript
复制
import heapq

class Ordered:

    def __init__(self,aset):
        self.aset = aset

    def __iter__(self):
        # memorize original elements
        known = set(self.aset)
        last = None

        # create heap
        heap = list(self.aset)
        heapq.heapify(heap)

        # yield from heap
        while heap:
            v = heapq.heappop(heap)
            if (v in self.aset and  # not removed
                    (last is None or last < v)): # not smaller than last
                yield v
            last = v

            # in case of new elements, update the heap
            diff = self.aset - known
            if diff:
                for x in self.aset - known:
                    heapq.heappush(heap, x)
                known = set(self.aset)

这适用于您的所有测试用例(未显示si的重新初始化):

代码语言:javascript
复制
>>> s = {1, 2, 4, 8, 16}
>>> i = iter(Ordered(s))
>>> [next(i), next(i), s.remove(8), next(i), next(i), s.add(32), next(i)]
[1, 2, None, 4, 16, None, 32]
>>> [next(i), next(i), next(i), s.add(3), next(i), s.add(10), s.add(32), next(i), next(i), next(i)]
[1, 2, 4, None, 8, None, None, 10, 16, 32])
>>> [next(i), s.remove(2), s.remove(4), s.remove(8), next(i)]
[1, None, None, None, 16]
>>> [next(i), s.remove(2), next(i), s.remove(4), s.remove(8), next(i)]
[1, None, 4, None, None, 16]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40297205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档