Python :一个for循环无法实现,出现“x not in list”,菜鸟一个,请问大神怎么回事?
>>>a=[1,3,3,4,5,6,3343,52]>>>ad=[]>>>importrandom>>>foriinrange(len(a)):...print(rand...
>>> a= [1, 3, 3, 4, 5, 6, 3343, 52]
>>> ad = [ ]
>>> import random
>>> for i in range(len(a)):
... print (random.choice(a))
... ad = a.remove(i)
... print (ad)
... print (a)
... a = ad
...
52
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
ValueError: list.remove(x): x not in list 展开
>>> ad = [ ]
>>> import random
>>> for i in range(len(a)):
... print (random.choice(a))
... ad = a.remove(i)
... print (ad)
... print (a)
... a = ad
...
52
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
ValueError: list.remove(x): x not in list 展开
1个回答
展开全部
a= [1, 3, 3, 4, 5, 6, 3343, 52]
ad = [ ]
import random
for i in range(len(a)): # range(8), i = [0,1,2,3,4,5,6,7]
print (random.choice(a))
ad = a.remove(i) # when i =2, i not in a, Error!
print (ad)
print (a)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询