slicing insert question, L[1:1]

前端 未结 3 893
自闭症患者
自闭症患者 2021-01-14 16:18

practising some python, which is a pretty easy language to grab up.

I have

>>> L = [1,2,3,4]
>>> L[1:1] = [1,2,3]
>>> L
[1         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-14 16:34

    L[1:1] means the slice of the list L starting at index 1 (the second element), up to but not including index 1. So it is an empty list. On the right-hand side of an assignment, it is simply an anonymous empty list. But on the left-hand side, the assignment knows where the slice has been made, and can splice in the new list value into the proper place.

提交回复
热议问题