How to copy Python lists: avoid a common pitfall

Опубликовано: 09 Сентябрь 2023
на канале: PatchyPython
74
0

Python list names are actually pointers or references to a memory area, so a copy of a list obtained by a simple assignment is basically only another name for it. This is means that altering the copy will also change the original, which is not what you may expect. While this is convenient for efficiency reasons, if you want to edit your copy you will normally want it to be independent of the original. The slicing operator comes to the rescue here, as it allows you to create an actual, independent copy of the list, which keeps its own independent catalogue of all the elements. For more information, see the notebooks below (notebooks will open on Google Colab - no installation required)

Embedded video, part of the following courses:
Computer Programming at Queen Mary, University of London
Jupyter notebook: https://tinyurl.com/3xjmpf94
Coding for Scientists at Queen Mary, University of London
Jupyter notebook: https://tinyurl.com/yedumefr


Смотрите видео How to copy Python lists: avoid a common pitfall онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь PatchyPython 09 Сентябрь 2023, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 74 раз и оно понравилось 0 людям.