Python list.remove() skips next element in list

前端 未结 6 1732
夕颜
夕颜 2020-12-16 01:27

Python, but not programming, newbie here. I\'m programming with lists and have run into an interesting problem.

width = 2
height = 2

# Traverse the board
de         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 01:47

    Removing array elements inside for loop is not advisable because for loop checks indexes in a order and removing an element inside loop shifts proceeding elements left one index, if you want to do it then use reversed()

    arr = [1,2,3,4,5]

    for ele in reversed(arr): condition: arr.remove(ele)

提交回复
热议问题