简体   繁体   中英

How do I make a copy of a list without changing its contents?

However, when I change new, it changes self._marker as well. I want to be able to change new, without it changing self._marker. I cannot use deepcopy either, since I do this operation several thousand times, and incur a huge performance penalty for using deepcopy.

What do I do?

You are on the right track, but your proposed solution does not work because you do not make a copy of the inner lists. They stay the same.

You can do this:

new_list = [e[:] for e in self._marker]

This performa a deepcopy which is what you need in that case.

This answers the question you asked. If you have other requirements, you need to make them explicit in your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM