What does [:] mean?

前端 未结 6 1566
时光取名叫无心
时光取名叫无心 2020-11-27 02:33

I\'m analyzing some Python code and I don\'t know what

pop = population[:]

means. Is it something like array lists in Java or like a bi-di

6条回答
  •  囚心锁ツ
    2020-11-27 03:06

    It is an example of slice notation, and what it does depends on the type of population. If population is a list, this line will create a shallow copy of the list. For an object of type tuple or a str, it will do nothing (the line will do the same without [:]), and for a (say) NumPy array, it will create a new view to the same data.

提交回复
热议问题