3 回答

TA貢獻1828條經驗 獲得超13個贊
迭代列表時不應從列表中刪除元素。相反,使用列表理解:
x=[e for e in x if e] y=[e for e in y if e]

TA貢獻1856條經驗 獲得超17個贊
“remove”方法僅刪除第一次出現的提供值:
def remove(self, *args, **kwargs): # real signature unknown
"""
Remove first occurrence of value.
Raises ValueError if the value is not present.
"""
pass
要從列表中刪除所有值,您可以使用以下小方法:
def remove_all_values_from_list(l, val):
for i in range(l.count(val)):
l.remove(val)
輸出:
[-0.9000000000000199, 2.1499999999999773, 1.799999999999983, -1.5000000000000284, -2.3500000000000227, -3.05000000000004, 2.0999999999999943, 3.9999999999999716, 1.8499999999999943, -4.650000000000006, 11.349999999999994]
[-5.750000000000028]
添加回答
舉報