def switch(g, p,n):
final = []
for i in range(len(p)):
d = list(range(n))
d.remove(g[i])
d.remove(p[i])
final.append(d)
return final
switch([2, 3, 0], [[1, 3, 4, 5, 6, 7, 8, 9], [0, 2, 4, 5, 6, 7, 8,
9],[1, 2, 4, 5, 6, 7, 8, 9]],11)
但当我运行此代码时,会出现以下错误:
ValueError Traceback (most recent call last) <ipython-input-151-72e1cc5c9abf> in <module>()
10 return final
11
---> 12 switch([2, 3, 0], [[1, 3, 4, 5, 6, 7, 8, 9], [0, 2, 4, 5, 6, 7, 8, 9],[1, 2, 4, 5, 6, 7, 8, 9]],11)
<ipython-input-151-72e1cc5c9abf> in switch(g, p, n)
4 d = list(range(n))
5 d.remove(g[i])
----> 6 d.remove(p[i])
7
8 final.append(d)
ValueError: list.remove(x): x not in list
我做错什么了?我只想把g和p的数字从列表中删除,然后得到作为输出的数字。你知道吗
我们不是从列表中删除项,而是只使用未排除的项创建子列表。你知道吗
相关问题 更多 >
编程相关推荐