[Python] Precautions when assigning values to multidimensional arrays

What I wanted to do

I was trying to substitute a number (60) for an initialized two-dimensional array (dp) due to a problem with dynamic programming.

[Problem] Atcoder EDPC C-Vacation (https://atcoder.jp/contests/dp/tasks/dp_c)

I wrote the following code.

dp=[[0,0,0]]*5
print(dp)
#output[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]]

a=[10,40,70]
dp[0]=a
print(dp)
#output[[10,40,70],[0,0,0],[0,0,0],[0,0,0],[0,0,0]]


#Below is the code in question#######
dp[0][1]=60
print(dp)
#output[[10,40,70],[0,60,0],[0,60,0],[0,60,0],[0,60,0]]

I really wanted to substitute 60 only for dp [0] [1]. (Less than)


print(dp)
#output[[10,40,70],[0,60,0],[0,0,0],[0,0,0],[0,0,0]]

Cause

If the list is initialized as follows, all the element lists will be created as the same object. (Less than)

#Bad example 1
dp=[[0,0,0]]*5

#Bad example 2
dp=[[0]*3]*5

Solution

If you initialize the list with " Comprehension notation </ b>" as shown below, all can be created as different objects.


dp=[[0]*3 for i in range(5)]
dp[0][1]=60
print(dp)
#output[[10,40,70],[0,60,0],[0,0,0],[0,0,0],[0,0,0]]

numpy makes it easy

import numpy as np

dp=np.zeros((5,3))
dp[0,1]=60
print(dp)
#Output array([[ 0.,  0.,  0.],[ 0., 60.,  0.],[ 0., 0.,  0.],[ 0., 0.,  0.],[ 0., 0.,  0.]])
       

Summary

If you want to initialize a multidimensional array with list type, use comprehension notation. However, it is easier to initialize with numpy.

reference

https://note.nkmk.me/python-list-initialize/

Recommended Posts

[Python] Precautions when assigning values to multidimensional arrays
Precautions when giving default values to arguments in Python function definitions
Python Note: When assigning a value to a string
Tips and precautions when porting MATLAB programs to Python
Precautions when upgrading TensorFlow (to 1.3)
Call C language functions from Python to exchange multidimensional arrays
Precautions when using pit in Python
Check the behavior when assigning Python
[Python] How to swap array values
Precautions when creating a Python generator
Precautions when using phantomjs from python
Precautions when using six with Python 2.5
Precautions when passing def to sorted and groupby functions in Python? ??
Precautions when pickling a function in python
[Introduction to Udemy Python 3 + Application] 10. Numerical values
Precautions when solving DP problems with Python
How to start Python (Flask) when EC2 starts
[Introduction to Udemy Python 3 + Application] 38. When judging None
Convert Windows epoch values to date with python
Precautions when dealing with control structures in Python 2.6
What I did when updating from Python 2.6 to 2.7
Error when trying to install psycopg2 in Python
[Web development with Python] Precautions when saving cookies
Materials to read when getting started with Python
Updated to Python 2.7.9
Python: Tips-Swap values
Python multidimensional array
Access order of multidimensional arrays when calling Fortran subroutines from Python (ctypeslib and f2py)
"Backport" to python 2
[Python] How to remove duplicate values from the list
When I tried to introduce python3 to atom, I got stuck
Things to keep in mind when developing crawlers in Python
Note assigning image textures to materials in Maya python
Precautions when dealing with ROS MultiArray types in Python
Things to keep in mind when copying Python lists
Python --Notes when converting from str type to int type
Things to note when initializing a list in Python
Specifies the function to execute when the python program ends
How to exit when using Python in Terminal (Mac)
Articles to see when installation for Python + OpenCV fails
What to do when "cannot import name xxx" [Python]
Only size-1 arrays can be converted to Python scalars
How to retrieve multiple arrays using slice in python.
Things to keep in mind when processing strings in Python2
[Python] How to output the list values in order
What I was addicted to when using Python tornado
Things to keep in mind when processing strings in Python3
I want to do something in Python when I finish
Python Note: The mystery of assigning a variable to a variable
What to do when you can't bind CaboCha to Python
Precautions when inputting from CSV with Python and outputting to json to make it an exe