Create Python two-dimensional array correctly

Java creates two-dimensional array#

Let me take a look at the format of Java to create a two-dimensional array:

type arrayName[][];
type [][]arrayName;

such as:

int [][]  arr=newint[5][3];

A two-dimensional array of integers with 5 rows and 3 columns is created. Freehand...

The embarrassment of creating a Python two-digit array#

I also want to imitate Java, but I can only:

>>> li[][]=[][]
 File "<stdin>", line 1
 li[][]=[][]^
SyntaxError: invalid syntax

Sorry, it doesn't work.

But it can be like this,

>>> li =[[1,2,3],[4,5,6]]>>> li
[[1,2,3],[4,5,6]]

This is too troublesome, I wonder if there is a better way.

Python two-dimensional arrays you think#

method one:

>>> rows, cols =(2,3)>>> arr =[[0]* cols]* rows
>>> arr
[[0,0,0],[0,0,0]]

Method Two:

>>> arr =[[0for i inrange(cols)]for j inrange(rows)]>>> arr
[[0,0,0],[0,0,0]]

Both methods provide the same output as now.

Change the element to see##

Let's change the elements in the arrays of method one and method two:

# method one
>>> rows, cols =(2,3)>>> arr =[[0]* cols]* rows
>>> arr[0][0]=1>>>for row in arr:...print(row)...[1,0,0][1,0,0]

A strange thing happened, obviously I only changed arr[0][0], what I want is that the first element of the first row is changed to 1, but the first element of each row is changed to 1.

# Method Two
>>> rows, cols =(2,3)>>> arr =[[0for i inrange(cols)]for j inrange(rows)]>>> arr[0][0]=1>>>for row in arr:...print(row)...[1,0,0][0,0,0]

Method two is exactly the answer I want.

What's the reason?

It's all to blame for the shallow copy of Python. If you still don't understand shallow copy and deep copy, take a look at this Learning Python for a year, this time I finally understand shallow copy and deep copy.

I will briefly explain here:

In method 1, Python will not create two list objects, but only one list object, and all indexes of the array arr point to the same list object (list), as shown in the figure.

Insert picture description here

Method two, will create 2 separate list objects, as shown below:

Insert picture description here

So the correct way is to use method two,

That is

rows, cols =(5,5)
arr2 =[[0for i inrange(cols)]for j inrange(rows)]

A two-dimensional array with 5 rows and 5 columns is created successfully:

>>> for row in arr2:...print(row)...[0,0,0,0,0][0,0,0,0,0][0,0,0,0,0][0,0,0,0,0][0,0,0,0,0]

However, it's not over yet, there is one more thing, we found that i and j seem to be unused, so we use a single underscore _:

rows, cols =(5,5)
arr2 =[[0for _ inrange(cols)]for _ inrange(rows)]

Sometimes a single independent underscore is used as a name to indicate that a variable is temporary or irrelevant.

to sum up#

At this point, we are finally able to correctly create a Python two-dimensional array. Yes, it is:

arr2 =[[0for _ inrange(5)]for _ inrange(5)]  #Create a correct array of 5 rows and 5 columns

Recommended Posts

Create Python two-dimensional array correctly
python print array
python array add array_Python add array
Python entry tutorial notes (3) array
Python implements multi-dimensional array sorting
Python one-dimensional two-dimensional interpolation example