2

我刚刚遇到了一个TypeError我以前从未见过的问题,并且无法弄清楚它为什么会发生。谷歌搜索错误TypeError: 'Zero' object is not iterable不会返回任何结果。我在 python 2.7 和 3.5 中测试过,两种情况下的错误都是一样的。

这是MCVE:

from sympy.geometry.polygon import Polygon
import pyclipper as pc

start_list = [(0, 2), (2, 2), (2, 0), (0, 0)]
scaled = pc.scale_to_clipper(start_list)  # this works fine

as_poly = Polygon(*start_list)
new_list = [(pt.x, pt.y) for pt in as_poly.vertices]
assert new_list == start_list  # check that the lists are the same (this passes)

fail_to_scale = pc.scale_to_clipper(new_list)  # this fails

和追溯:

Traceback (most recent call last):
  File "C:\Users\Jamie\<blah>\mcve.py", line 10, in <module>
    fails = pc.scale_to_clipper(new_list)
  File "pyclipper/pyclipper.pyx", line 544, in pyclipper.scale_to_clipper (pyclipper/pyclipper.cpp:3535)
  File "pyclipper/pyclipper.pyx", line 542, in pyclipper.scale_to_clipper.scale_value (pyclipper/pyclipper.cpp:3454)
  File "pyclipper/pyclipper.pyx", line 542, in pyclipper.scale_to_clipper.scale_value (pyclipper/pyclipper.cpp:3454)
  File "pyclipper/pyclipper.pyx", line 542, in pyclipper.scale_to_clipper.scale_value (pyclipper/pyclipper.cpp:3416)
TypeError: 'Zero' object is not iterable

有谁知道这个错误的来源和解决方案可能是什么?

4

1 回答 1

2

这已在 PyClipper 版本 1.0.2 中得到修复,这比使用下面描述的解决方法更可取

好的,我已经将问题追溯到存储在Polygon. 问题是该值0由 SymPy 存储为sympy.Zero.

这些sympy.Zero值不被接受polyclipper.scale_to_clipper(),因此TypeError在检查时会提高。

为了避免这个问题,我可以生成强制new_list转换pt.x并:pt.yfloat

new_list = [(float(pt.x), float(pt.y)) for pt in as_poly.vertices]
于 2016-08-16T09:17:52.283 回答