Step on Python's Shallow Copy

Artificial intelligence is very popular recently, and Python will inevitably follow.
One day I happened to see this passage on Weibo:

@bingdongwinter: Let me talk about two new standards: write a dynamic programming (maximum subsection sum) and then talk about the algorithm, and write a small demo of one of the three algorithms of simulated annealing, genetics and neural network and talk about AI. Isn't that too much?

Since I myself pay more attention to the basics, I quite agree with this sentence. Although algorithm engineers often laugh at themselves as "parameter tuning engineers", they are also laughing at themselves. For example, R&D engineers who write engineering codes can also laugh at themselves as "brick-moving engineers". But to be honest, if I really take this kind of statement seriously, it is estimated that if the bricks are not moved properly, it will only add to the chaos.

So I was thinking, it is better to pick up the rusty Python skills for a long time, first to brush the DP questions to practice, and then to take a deep look at the field of artificial intelligence.

Looking back at the Python blog post I wrote before , it was many years ago. At that time, I still encountered some problems when I switched from 2.7 to 3. I didn’t expect that after so many years, the share of Python 2.7 was still so large, and I couldn’t help feeling emotional. down .

But after so many years, except for the vague PVM memory and recorded blog posts, Python 2.7 and Python 3 are the same for me, and I think I should start with Python 3.5.

Back to the topic.
In the process of writing questions, Python is used to construct two-dimensional arrays, although the first thing that floats in my mind is arrays of unequal lengths, such as:

[
[1]
[2, 3]
[4, 5, 6]
]

After all, Python is the same as Objective-C, everything is an object, and all PyObjects are placed in [], it doesn’t matter how long or short it is.

However, due to the inertia of writing the C language, I still pre-initialized the N*N array when I wrote the code: routes = [[0] * n] * n, compared to the array elements with inconsistent lengths, it can avoid out-of-bounds operations.

As a result, I encountered a problem when doing the write operation:

routes = [[0] * n] * n
routes[0][0] = 1
print(routes)
输出了:[[1, 0, 0], [1, 0, 0], [1, 0, 0]]

The first reaction is still a bit confusing, because according to the logic of the C language, each element is located at a different address, how can a line of assignment statement write three different addresses?

Until I saw the two words Shallow Copy later , I instantly recalled that I stepped on this pit when I first wrote Python many years ago.

I think this is the optimization that Python, an interpreted scripting language, is trying to pursue because of the slow performance (compared to C language and the like) brought about by the introduction of a virtual machine like PVM.

If you want to pre-initialize the two-dimensional array of Deep Copy, you can write: routes = [([-1] * n) for i in range(n)], but referring to the optimization attitude of Python, it may be better to append array elements of different lengths when needed? That is, would be more pythonic ?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325514409&siteId=291194637