In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Python array practice problem analysis, for this problem, this article details the corresponding analysis and solution, hoping to help more small partners who want to solve this problem find a simpler and easier way.
Python arrays have a lot of knowledge that requires detailed study. Python arrays do not have array data structures, but lists look like arrays, such as:
a=[0,1,2] In this case a[0]=0, a[1]=1, a[[2]=2, but this leads to the question of what to do if the array a wants to be defined as 0 to 999, which can be achieved by a = range(0, 1000). or omitted as a = range(1000). If you want to define a with a length of 1000 and all initial values are 0, then a = [0 for x in range(0, 1000)] Here is the definition of a two-dimensional array:
A=[[1,1],[1,1]] is defined directly, where a 2*2 two-dimensional array is defined, and the initial value is 0.
Indirectly define a=[[0 for x in range(10)] for y in range(10)], where 10*10 is defined as a two-dimensional array initially 0.
Later, I found a simpler way to use two-dimensional arrays online:
b = [[0]*10]*10, defines a two-dimensional array of 10*10 initially 0.
Compare with a=[0 for x in range(10)] for y in range(10)]: print a==b results in True.
However, after replacing a with the definition method of b, the previous program that can run normally is also wrong. After careful analysis, the difference is obtained:
A[0][0]=1, only a[0][0] is 1, and all others are 0.
[1][2][3][4][5][6][7][8][9][9][9][10][11][12][13][14][15][16][17][18][19][19][19]
The result is that the 10 small one-dimensional data in the large array are all the same reference, that is, pointing to the same address.
Therefore b = [[0]*10]*10 does not correspond to a two-dimensional array in our conventional sense.
At the same time, after testing: the definition of c=[0]*10 has the same effect as c=[0 for x in range(10)], without the same problem of reference above, it is estimated that the definition of Python array c is value type multiplication, while the previous b is multiplied by type, because the one-dimensional array is a reference (borrowing the value type and reference type in C#, I wonder if it is appropriate).
About Python array practice problem analysis questions to share here, I hope the above content can be of some help to everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.