Does [:] slice only make shallow copy of a list?

前端 未结 5 1324
Happy的楠姐
Happy的楠姐 2020-12-10 08:41

I have experienced peculiar bugs from this [:] copy.

The docs say [:] makes only a shallow copy but seems:

a = [1,2,3]
id(         


        
5条回答
  •  情歌与酒
    2020-12-10 09:47

    Numpy defines what what a slice returns differently to the standard python library. This is because numpy is made to work with huge amounts of data. Copying these huge arrays is not always wanted, especially if the user only wanted a temporary view of the array. For instance, arr[:100].sum() sums the first 100 elements, without having to create a temporary shallow copy of the first 100 elements. Note that temporary views are only created for a basic slice.

    See the documentation for more details.

提交回复
热议问题