0
我需要在[-n,-1] union [1,n]中取样k个数字而无需替换。为什么这个代码不工作?Python样本没有0
random.sample(range(-n,n+1).remove(0),k)
我得到
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/python2.7/random.py", line 319, in sample
n = len(population)
TypeError: object of type 'NoneType' has no len()
这可以在调用'sample'内完成吗?那就是'sample(某个代码,k)'? –
谢谢,那就是我一直在寻找的 –