[英]AssertionError of different lists
I made a reverse enumerate without the use of the reversed() function.我在不使用 reversed() 函数的情况下进行了反向枚举。 I want the string and the numbers to be printed in reverse.我希望字符串和数字反向打印。 However, when testing, I get an AssertionError stating that the lists do not match.但是,在测试时,我收到一个 AssertionError,指出列表不匹配。
def my_own_enumerate(a):
for i in range(len(a)-1, -1, -1):
return i, a[i]
class Test(unittest.TestCase):
def test_my_own_enumerate(self):
self.assertEqual([(0, "m"), (1, "e"), (2, "!")], list(my_own_enumerate("me!")))
my_own_enumerate
is meant to return a reversed enumeration (as you stated yourself) so I'm not sure why you assert its output with a non-reversed enumeration. my_own_enumerate
是为了返回一个反向枚举(正如你自己所说的),所以我不确定你为什么用一个非反向枚举来断言它的输出。
The loop in my_own_enumerate
returns after the first iteration, so it will always return only the last character and its index. my_own_enumerate
的循环在第一次迭代后返回,因此它总是只返回最后一个字符及其索引。 Instead, try相反,尝试
def my_own_enumerate(a): return [(i, a[i]) for i in range(len(a) - 1, -1, -1)]
Then然后
print(my_own_enumerate('me!'))
# [(2, '!'), (1, 'e'), (0, 'm')]
And和
class Test(unittest.TestCase):
def test_my_own_enumerate(self):
self.assertEqual([(2, '!'), (1, 'e'), (0, 'm')], my_own_enumerate("me!"))
Does not fail.不会失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.