ValueError: Sample larger than population or is negative (with n=5)
ValueError: Sample larger than population or is negative (with n=5)
我写了一个代码来随机获取一组人。目的是当一个人已经被随机选择时,程序应该删除他。我使用了 random.sample()
函数,它在 n=1,2,3,4
上运行良好,当我达到 5 时,它给了我一个错误,直到现在我试图了解这个函数背后发生的事情。任何解释和提示都会有帮助。谢谢!
import random
ma_list =["anne","aline","gros","eve","armand","yves","elv","allo","sonia","luc","marc","jules","kevin"]
#this will contain an occurence of our list
maListOc = ma_list
#this list will contain our random list
random_list = None
groupe = 1
#for each element in ma_list, we randome and put into our variable
for i in ma_list:
random_list = random.sample(ma_list, 5)
#then we remove data already randomize in our list, but the complexity is high for this little program
for element in random_list:
ma_list.remove(element)
print("Goupe N°:",groupe)
#and we finally print our randomized list
print(random_list)
print("______________________________")
groupe += 1
print(maListOc)
这是输出:
Goupe N°: 1
['aline', 'gros', 'armand', 'sonia', 'anne']
______________________________
Goupe N°: 2
['kevin', 'allo', 'eve', 'elv', 'marc']
______________________________
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-29-c2d13d61c8d1> in <module>
8 #for each element in ma_list, we randome and put into our variable
9 for i in ma_list:
---> 10 random_list = random.sample(ma_list, 5)
11 #then we remove data already randomize in our list, but the complexity is high for this little program
12 for element in random_list:
~\anaconda3\lib\random.py in sample(self, population, k)
361 n = len(population)
362 if not 0 <= k <= n:
--> 363 raise ValueError("Sample larger than population or is negative")
364 result = [None] * k
365 setsize = 21 # size of a small set minus size of an empty list
ValueError: Sample larger than population or is negative
错误显示 ma_list 的 len 小于 5。您需要添加如下 if... 来打破 for 循环。
import random
ma_list =["anne","aline","gros","eve","armand","yves","elv","allo","sonia","luc","marc","jules","kevin"]
#this will contain an occurence of our list
maListOc = ma_list
#this list will contain our random list
random_list = None
groupe = 1
#for each element in ma_list, we randome and put into our variable
for i in ma_list:
if(len(ma_list)<=5):
break
random_list = random.sample(ma_list, 5)
#then we remove data already randomize in our list, but the complexity is high for this little program
for element in random_list:
ma_list.remove(element)
print("Goupe N°:",groupe)
#and we finally print our randomized list
print(random_list)
print("______________________________")
groupe += 1
print(maListOc)
输出:
Goupe N°: 1
['aline', 'jules', 'marc', 'yves', 'luc']
______________________________
Goupe N°: 2
['kevin', 'armand', 'gros', 'anne', 'sonia']
______________________________
['eve', 'elv', 'allo']
我写了一个代码来随机获取一组人。目的是当一个人已经被随机选择时,程序应该删除他。我使用了 random.sample()
函数,它在 n=1,2,3,4
上运行良好,当我达到 5 时,它给了我一个错误,直到现在我试图了解这个函数背后发生的事情。任何解释和提示都会有帮助。谢谢!
import random
ma_list =["anne","aline","gros","eve","armand","yves","elv","allo","sonia","luc","marc","jules","kevin"]
#this will contain an occurence of our list
maListOc = ma_list
#this list will contain our random list
random_list = None
groupe = 1
#for each element in ma_list, we randome and put into our variable
for i in ma_list:
random_list = random.sample(ma_list, 5)
#then we remove data already randomize in our list, but the complexity is high for this little program
for element in random_list:
ma_list.remove(element)
print("Goupe N°:",groupe)
#and we finally print our randomized list
print(random_list)
print("______________________________")
groupe += 1
print(maListOc)
这是输出:
Goupe N°: 1
['aline', 'gros', 'armand', 'sonia', 'anne']
______________________________
Goupe N°: 2
['kevin', 'allo', 'eve', 'elv', 'marc']
______________________________
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-29-c2d13d61c8d1> in <module>
8 #for each element in ma_list, we randome and put into our variable
9 for i in ma_list:
---> 10 random_list = random.sample(ma_list, 5)
11 #then we remove data already randomize in our list, but the complexity is high for this little program
12 for element in random_list:
~\anaconda3\lib\random.py in sample(self, population, k)
361 n = len(population)
362 if not 0 <= k <= n:
--> 363 raise ValueError("Sample larger than population or is negative")
364 result = [None] * k
365 setsize = 21 # size of a small set minus size of an empty list
ValueError: Sample larger than population or is negative
错误显示 ma_list 的 len 小于 5。您需要添加如下 if... 来打破 for 循环。
import random
ma_list =["anne","aline","gros","eve","armand","yves","elv","allo","sonia","luc","marc","jules","kevin"]
#this will contain an occurence of our list
maListOc = ma_list
#this list will contain our random list
random_list = None
groupe = 1
#for each element in ma_list, we randome and put into our variable
for i in ma_list:
if(len(ma_list)<=5):
break
random_list = random.sample(ma_list, 5)
#then we remove data already randomize in our list, but the complexity is high for this little program
for element in random_list:
ma_list.remove(element)
print("Goupe N°:",groupe)
#and we finally print our randomized list
print(random_list)
print("______________________________")
groupe += 1
print(maListOc)
输出:
Goupe N°: 1
['aline', 'jules', 'marc', 'yves', 'luc']
______________________________
Goupe N°: 2
['kevin', 'armand', 'gros', 'anne', 'sonia']
______________________________
['eve', 'elv', 'allo']