所以我得到了这个功能。不得更改!
class TestUnikati(unittest.TestCase):
def test_02_clean(self):
s = [5, 4, 1, 4, 4, 4, 4, 4, 3]
unique_array(s) #<-- calls the function
self.assertEqual(s, [5, 4, 1, 3])
基本上我们测试函数 clean() 是否只返回一个唯一的数组。变量 s 是数组。 这是我的函数,它获取困惑的数组 s 并尝试返回一个没有重复元素的数组
def unique_array(s):
s=unique(s) #<-- this is a function that just returns a unique array
x=TestUnikati() #<-- I store the class in the x variable
[insert a way to push s to the "TestUnikati.test_02_clean.s"]
我尝试了很多东西。我已经用 globals() 和 locals() 以及 x 对象变量尝试了一些实验,但我似乎没有做对。
我尝试使用 x 对象将它推送到 TestUnikati.test_02_clean.s 的 locals()。有没有办法保存它,这样类函数中的 s 将被覆盖并且 self.assertEqual(s, [5, 4, 1, 3]) 将比较 2 并通过它?像这样:
x.test_02_clean.s=unique(s)
or
x.s=unique(s)
请您参考如下方法:
正如其他人所说,由于 test_02_clean() 没有将调用 unique_array() 的结果分配给任何东西,它希望您改变给定的列表到位。像这样:
def unique_array(s):
# Keep a set of all the items we've seen so far
seen = set()
# Index into list
i = 0
while i < len(s):
if s[i] in seen:
# Delete element from list if we've already seen it
del s[i]
else:
# Or else add it to our seen list and increment the index
seen.add(s[i])
i += 1
>>> s = [5, 4, 1, 4, 4, 4, 4, 4, 3]
>>> unique_array(s)
>>> s
[5, 4, 1, 3]
