slicing insert question, L[1:1]

前端 未结 3 896
自闭症患者
自闭症患者 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:30

    Slicing behaves differently depending on whether it's on the left- or right-hand side of an expression. When it's on the left side, it doesn't return a list - instead, it behaves as a slice object, which knows more about slices and has assignment specifically overridden to operate as insertion.

提交回复
热议问题