我在使用timeit模块时遇到了困难,这是我的代码。
import timeit
setup = """
a = range(100000)
b = set(a)
"""
print timeit.timeit("a.remove(0)", setup=setup, number = 10)
print timeit.timeit("b.remove(0)", setup=setup, number = 10)
最后一行分别给出了ValueError和KeyError。就像我输入的价值观从未存在过一样。但是当我使用print a
或print b
时。它可以毫无问题地打印值。这可能是我的错误吗?
修改
这是第一个错误。
Traceback (most recent call last):
File "C:\Users\ata\Desktop\Learn\graph theory\try.py", line 18, in <module>
print timeit.timeit("a.remove(0)", setup=setup, number = 10)
File "C:\Python27\lib\timeit.py", line 237, in timeit
return Timer(stmt, setup, timer).timeit(number)
File "C:\Python27\lib\timeit.py", line 202, in timeit
timing = self.inner(it, self.timer)
File "<timeit-src>", line 9, in inner
ValueError: list.remove(x): x not in list