迭代时zip和izip之间有什么功能上的区别吗?

可能是

我有如下代码:

for v1, v2 in zip(iter1, iter2):
   print len(v1) # prints 0

但是,当我将zip更改为itertools.izip时,它会打印1

for v1, v2 in izip(iter1, iter2):
   print len(v1) # prints 1

其他所有代码都是相同的。我只是用izip替换了zip,它就起作用了。izip的输出是正确的。

编辑:添加整个代码:

#!/bin/python

"""
How to use:
>>> from color_assign import Bag, assign_colors
>>> from pprint import pprint
>>> old_topics = set([
... Bag(name='T1', group=0, color=1, count=16000),
... Bag(name='T2', group=0, color=1, count=16000),
... Bag(name='T3', group=1, color=2, count=16000),
... Bag(name='T4', group=2, color=3, count=16000),
... ])
>>> new_topics = set([
... Bag(name='T1', group=0, color=None, count=16000),
... Bag(name='T2', group=4, color=None, count=16000),
... Bag(name='T3', group=1, color=None, count=16000),
... Bag(name='T4', group=1, color=None, count=16000),
... ])
>>> color_ranges = [ [1,10] ]
>>> assign_colors(old_topics, new_topics, color_ranges)
>>> pprint(sorted(new_topics, key=attrgetter('name')))
[Bag(name=T1, group=0, color=1, count=16000),
 Bag(name=T2, group=4, color=3, count=16000),
 Bag(name=T3, group=1, color=2, count=16000),
 Bag(name=T4, group=1, color=2, count=16000)]
>>> 
"""

from itertools import groupby, izip
from operator import attrgetter

class Bag:
  def __init__(self, name, group, color=None, count=None):
    self.name  = name 
    self.group = group
    self.color    = color   
    self.count  = count 
  def __repr__(self):
    return "Bag(name={self.name}, group={self.group}, color={self.color}, count={self.count})".format(self=self)
  def __key(self):
    return self.name
  def __hash__(self):
    return hash(self.__key())
  def __eq__(self, other):
    return type(self) is type(other) and self.__key() == other.__key()

def color_range_gen(color_ranges, used_colors):
  color_ranges = sorted(color_ranges)
  color_iter = iter(sorted(used_colors))
  next_used = next(color_iter, None)
  for start_color, end_color in color_ranges:
    cur_color = start_color
    end_color = end_color
    while cur_color <= end_color:
      if cur_color == next_used:
        next_used = next(color_iter, None)
      else:
        yield cur_color
      cur_color = cur_color + 1


def assign_colors(old_topics, new_topics, color_ranges):
  old_topics -= (old_topics-new_topics) #Remove topics from old_topics which are no longer present in new_topics
  used_colors = set()

  def group_topics(topics):
    by_group = attrgetter('group')
    for _, tgrp in groupby(sorted(topics, key=by_group), by_group):
      yield tgrp

  for topic_group in group_topics(old_topics):
    oldtset = frozenset(topic_group)
    peek = next(iter(oldtset))
    try:
      new_group = next(topic.group for topic in new_topics if topic.name == peek.name and not topic.color)
    except StopIteration:
      continue
    newtset = frozenset(topic for topic in new_topics if topic.group == new_group)
    if oldtset <= newtset:
      for topic in newtset:
        topic.color = peek.color
      used_colors.add(peek.color)

  free_colors = color_range_gen(color_ranges, used_colors)
  unassigned_topics = (t for t in new_topics if not t.color)
  for tset, color in zip(group_topics(unassigned_topics), free_colors):
    for topic in tset:
      topic.color = color

if __name__ == '__main__':
  import doctest
  doctest.testmod()

用法:

my_host:my_dir$ /tmp/color_assign.py
**********************************************************************
File "/tmp/color_assign.py", line 21, in __main__
Failed example:
    pprint(sorted(new_topics, key=attrgetter('name')))
Expected:
    [Bag(name=T1, group=0, color=1, count=16000),
     Bag(name=T2, group=4, color=3, count=16000),
     Bag(name=T3, group=1, color=2, count=16000),
     Bag(name=T4, group=1, color=2, count=16000)]
Got:
    [Bag(name=T1, group=0, color=None, count=16000),
     Bag(name=T2, group=4, color=3, count=16000),
     Bag(name=T3, group=1, color=2, count=16000),
     Bag(name=T4, group=1, color=2, count=16000)]
**********************************************************************
1 items had failures:
   1 of   7 in __main__
***Test Failed*** 1 failures.
my_host:my_dir$ sed -i 's/zip(/izip(/g' /tmp/color_assign.py
my_host:my_dir$ /tmp/color_assign.py
my_host:my_dir$

更新:问题是groupby使用zip时使迭代器无效

user2357112支持Monica

您遇到的问题是由于两个因素的结合。首先,izip仅在需要时才推进基础迭代器,而zip需要立即获取所有项。其次,当groupby对象前进时,先前的迭代器不再有效

返回的组本身就是一个与共享底层可迭代对象的迭代器groupby()因为源是共享的,所以当groupby()对象前进时,上一个组将不再可见。因此,如果以后需要该数据,则应将其存储为列表:

作为一个简单的解决方法,您可以更改group_topicslist在产生它们之前调用它的组。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

[,] 2d数组和[[] []一个之间有什么功能上的区别吗?

来自分类Dev

[,] 2d数组和[[] []一个之间有什么功能上的区别吗?

来自分类Dev

取反时,“ x = -x”和“ x * = -1”之间在功能上有区别吗?

来自分类Dev

VS2012和VS2013解决方案之间在功能上有什么区别吗?

来自分类Dev

销毁对象中的这些代码之间在功能上有什么区别吗?

来自分类Dev

> *:first-child和>:first-child之间有功能上的区别吗?

来自分类Dev

AtomicInteger.updateAndGet()和AtomicInteger.accumulateAndGet()之间有什么功能上的区别?

来自分类Dev

这两个Angular控制器之间在功能上有什么区别吗

来自分类Dev

以下两个代码段之间有什么功能上的区别?

来自分类Dev

暂停/禁用Azure Logic App功能上的计时器-触发器和功能之间的区别吗?

来自分类Dev

F#中Array.replicate和Array.create之间是否有功能上的区别?

来自分类Dev

XML - 例如 <person name="JohnDoe"></person> 和 <person>JohnDoe</person> 在使用和功能上有什么区别?

来自分类Dev

表排序器分页器小部件和分页器插件在功能上有什么区别?

来自分类Dev

具有CheckBox的if语句的OnCheckedChangeListener或OnClickListener?功能上有什么区别?

来自分类Dev

在RethinkDB中使用“替换”与“更新”冲突解决方案有什么功能上的区别

来自分类Dev

为什么HTTPServlet是抽象类?有什么功能上的原因吗?

来自分类Dev

WPF和Winforms之间在性能上有区别吗?

来自分类Dev

OpenJDK和Oracle之间在性能上有区别吗?

来自分类Dev

rust迭代器的position()和rposition()之间有什么区别,有什么原因吗?

来自分类Dev

rust迭代器的position()和rposition()之间有什么区别,有什么原因吗?

来自分类Dev

我的代码的这两个摘要之间有功能上的区别吗?第二个与第一个以同样的方式处理可能的错误吗?

来自分类Dev

调用局部变量和调用对象的实例变量之间在性能上有什么区别吗?

来自分类Dev

$ @和“ $ @”之间有什么区别吗?

来自分类Dev

如何在功能上编写具有依赖条件的动态变化集合的迭代算法?

来自分类Dev

如何在功能上编写具有依赖条件的动态变化集合的迭代算法?

来自分类Dev

Python:`dist`和`sdist`之间在性能上有区别吗?

来自分类Dev

使用ARRAY []和'{}':: int []创建数组之间在性能上有区别吗?

来自分类Dev

Python:`dist`和`sdist`之间在性能上有区别吗?

来自分类Dev

ping和ping6之间在性能上有区别吗?

Related 相关文章

  1. 1

    [,] 2d数组和[[] []一个之间有什么功能上的区别吗?

  2. 2

    [,] 2d数组和[[] []一个之间有什么功能上的区别吗?

  3. 3

    取反时,“ x = -x”和“ x * = -1”之间在功能上有区别吗?

  4. 4

    VS2012和VS2013解决方案之间在功能上有什么区别吗?

  5. 5

    销毁对象中的这些代码之间在功能上有什么区别吗?

  6. 6

    > *:first-child和>:first-child之间有功能上的区别吗?

  7. 7

    AtomicInteger.updateAndGet()和AtomicInteger.accumulateAndGet()之间有什么功能上的区别?

  8. 8

    这两个Angular控制器之间在功能上有什么区别吗

  9. 9

    以下两个代码段之间有什么功能上的区别?

  10. 10

    暂停/禁用Azure Logic App功能上的计时器-触发器和功能之间的区别吗?

  11. 11

    F#中Array.replicate和Array.create之间是否有功能上的区别?

  12. 12

    XML - 例如 <person name="JohnDoe"></person> 和 <person>JohnDoe</person> 在使用和功能上有什么区别?

  13. 13

    表排序器分页器小部件和分页器插件在功能上有什么区别?

  14. 14

    具有CheckBox的if语句的OnCheckedChangeListener或OnClickListener?功能上有什么区别?

  15. 15

    在RethinkDB中使用“替换”与“更新”冲突解决方案有什么功能上的区别

  16. 16

    为什么HTTPServlet是抽象类?有什么功能上的原因吗?

  17. 17

    WPF和Winforms之间在性能上有区别吗?

  18. 18

    OpenJDK和Oracle之间在性能上有区别吗?

  19. 19

    rust迭代器的position()和rposition()之间有什么区别,有什么原因吗?

  20. 20

    rust迭代器的position()和rposition()之间有什么区别,有什么原因吗?

  21. 21

    我的代码的这两个摘要之间有功能上的区别吗?第二个与第一个以同样的方式处理可能的错误吗?

  22. 22

    调用局部变量和调用对象的实例变量之间在性能上有什么区别吗?

  23. 23

    $ @和“ $ @”之间有什么区别吗?

  24. 24

    如何在功能上编写具有依赖条件的动态变化集合的迭代算法?

  25. 25

    如何在功能上编写具有依赖条件的动态变化集合的迭代算法?

  26. 26

    Python:`dist`和`sdist`之间在性能上有区别吗?

  27. 27

    使用ARRAY []和'{}':: int []创建数组之间在性能上有区别吗?

  28. 28

    Python:`dist`和`sdist`之间在性能上有区别吗?

  29. 29

    ping和ping6之间在性能上有区别吗?

热门标签

归档