Python 如何解决错误:Zip 参数 #1 必须支持迭代

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17423779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:09:40  来源:igfitidea点击:

How to solve error: Zip argument #1 must support iteration

pythonunit-testingpython-unittest

提问by Johnnerz

I have two test methods with the same problem, here are the original methods in the main class:

我有两个测试方法有同样的问题,这里是主类中的原始方法:

def get_num_words(self, word_part):
    """ 1 as default, may want 0 as an invalid case """
    if word_part[3] == '0a':
        self.num_words = 10
    else:
        self.num_words = int(word_part[3])
    return self.num_words

def get_num_pointers(self, before_at):
    self.num_pointers = int(before_at.split()[-1])
    return self.num_pointers

And here are the two test classes:

这是两个测试类:

def test_get_num_words(self):
    word_part = ['13797906', '23', 'n', '04', 'flood', '0', 'inundation', '0', 'deluge', '0', 'torrent', '0', '005', '@', '13796604', 'n', '0000', '+', '00603894', 'a', '0401', '+', '00753137', 'v', '0302', '+', '01527311', 'v', '0203', '+', '02361703', 'v', '0101', '|', 'an', 'overwhelming', 'number', 'or', 'amount;', '"a', 'flood', 'of', 'requests";', '"a', 'torrent', 'of', 'abuse"']
    expected = 04
    real = self.wn.get_num_words(word_part)
    for r, a in zip(real, expected):
        self.assertEqual(r, a)

def test_get_num_pointers(self):
    before_at = '13797906 23 n 04 flood 0 inundation 0 deluge 0 torrent 0 005'
    expected = 5
    real = self.wn.get_num_pointers(before_at)
    for r, a in zip(real, expected):
        self.assertEqual(r, a)

This is the error they are giving out: TypeError: zip argument #1 must support iterationThe program works fully and these are the only 2 tests not working in 20 different tests.

这是他们给出的错误:TypeError: zip argument #1 must support iteration程序完全运行,这是仅有的 2 个测试在 20 个不同的测试中不起作用。

采纳答案by Martijn Pieters

Your gen_num_pointers()and gen_num_words()methods return an integer. zip()can only work with sequences (lists, sets, tuples, strings, iterators, etc.)

您的gen_num_pointers()gen_num_words()方法返回一个整数。zip()只能处理序列(列表、集合、元组、字符串、迭代器等)

You don't need to call zip()at all here; you are testing one integer against another:

你根本不需要zip()在这里打电话;您正在针对另一个测试一个整数:

def test_get_num_words(self):
    word_part = ['13797906', '23', 'n', '04', 'flood', '0', 'inundation', '0', 'deluge', '0', 'torrent', '0', '005', '@', '13796604', 'n', '0000', '+', '00603894', 'a', '0401', '+', '00753137', 'v', '0302', '+', '01527311', 'v', '0203', '+', '02361703', 'v', '0101', '|', 'an', 'overwhelming', 'number', 'or', 'amount;', '"a', 'flood', 'of', 'requests";', '"a', 'torrent', 'of', 'abuse"']
    self.assertEqual(4, self.wn.get_num_words(word_part))

def test_get_num_pointers(self):
    before_at = '13797906 23 n 04 flood 0 inundation 0 deluge 0 torrent 0 005'
    self.assertEqual(5, self.wn.get_num_pointers(before_at))

is plenty.

很多。

You also want to avoid using a leading 0on integer literals. 04is interpreted as an octalnumber; if you ever had to change that number to using more digits, or using digits outside of the range 0-7, you'd be in for a nasty surprise:

您还希望避免0在整数文字上使用前导。04被解释为八进制数;如果您不得不将该数字更改为使用更多数字,或使用 0-7 范围之外的数字,您会大吃一惊:

>>> 010
8
>>> 08
  File "<stdin>", line 1
    08
     ^
SyntaxError: invalid token

回答by Daniel Roseman

The error explains what is going on: the arguments to zip need to be iterables (ie lists, tuples, or something else you can actually iterate through). You are passing ints, ie single numbers.

该错误解释了正在发生的事情:zip 的参数必须是可迭代的(即列表、元组或其他您可以实际迭代的内容)。您正在传递整数,即单个数字。

I am not sure exactly what you are trying to do by calling zip, but maybe you just want to compare real and expected directly?

我不确定您通过调用 zip 究竟想做什么,但也许您只想直接比较真实和预期?

回答by Martin Maillard

Your test should look like this:

您的测试应如下所示:

def test_get_num_pointers(self):
    before_at = '13797906 23 n 04 flood 0 inundation 0 deluge 0 torrent 0 005'
    expected = 5
    real = self.wn.get_num_pointers(before_at)
    self.assertEqual(real, expected)

It only makes sense to use a forloop when you're asserting more than one value.

只有for在断言多个值时使用循环才有意义。