我遇到了一個問題,我的代碼中循環停止運行,一旦它從列表中刪除列表。遍歷列表python
data=[["why","why","hello"],["why","why","bell"],["why","hi","sllo"],["why","cry","hello"]]
for word_set in data:
if word_set[-1]!="hello":
data.remove(word_set)
print(data)
我期望的輸出是
[['why', 'why', 'hello'], ['why', 'cry', 'hello']]
但輸出
[['why', 'why', 'hello'], ['why', 'hi', 'sllo'], ['why', 'cry', 'hello']]
如何使循環下去,直到列表的末尾?
請添加一些評論,爲什麼你認爲這段代碼是答案,因爲它的立場,這個答案不是很有用。 –