The story that 2D list replacement did not work in python

Introduction

When I tried to replace a part of a 2D array in python, I made a mistake in the creation method and got stuck. As a little memorandum. ..

Problem description

It's very easy, but if you had a 2D array consisting of 5 * 4 0s, you wanted to replace some of them with 1.

#before:A two-dimensional array like this
[[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]]
'''''''''''''
      |
      |
      |
     \|/
'''''''''''''
#after:I wanted to replace it like this
[[0,0,0,0,0],
[0,0,1,0,0],
[0,0,0,0,0],
[0,0,0,0,0]]

what's happened?

First of all, from the mistake I made,

#Wrong example
#Create an initialized array
map_list = [[0]*W]*H

#map_Replace by specifying index of list
map_list[1][2] = 1

#Try to display the result
for line in map_list:
    print(line)

result:

[0, 0, 1, 0, 0]
[0, 0, 1, 0, 0]
[0, 0, 1, 0, 0]
[0, 0, 1, 0, 0]

Why! ?? You have specified the position properly! ?? ** For some reason executed for all columns **

In python you can check the id by the following method

print(id(a))

If you check the two-dimensional array earlier,

[1650962624, 1650962624, 1650962656, 1650962624, 1650962624]
[1650962624, 1650962624, 1650962656, 1650962624, 1650962624]
[1650962624, 1650962624, 1650962656, 1650962624, 1650962624]
[1650962624, 1650962624, 1650962656, 1650962624, 1650962624]

Apparently the cause is that the id of the column is covered.

solution

Now, the problem was the array creation part.

map_list = [[0]*5]*4

Create using list comprehension as follows

map_list = [[0 for i in range(5)] for r in range(4)]

Let's actually replace

#Create an initialized array
map_list = [[0 for i in range(5)] for r in range(4)]

#map_Replace by specifying index of list
map_list[1][2] = 1

#Try to display the result
for line in map_list:
    print(line)

output:

[0, 0, 0, 0, 0]
[0, 0, 1, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]

Summary

So, I was told that you have to be careful when initializing a two-dimensional array. There is a pitfall in an unexpected place.

Recommended Posts

The story that 2D list replacement did not work in python
The story that `while queue` did not work in python
The story that yapf did not work in vscode
The story that sendmail that can be executed in the terminal did not work with cron
[Python] Tensorflow 2.0 did not support Python 3.8, so the story of downgrading Python
The story of debugging in the local environment because the compilation did not work with Read the Docs
The story that the version of python 3.7.7 was not adapted to Heroku
The story of the release work of the application that Google does not tell
The story of exclude / include that Serverless Framework beginners misunderstood (did not understand) in beginner Pythonista
OR the List in Python (zip function)
A story that didn't work when I tried to log in with the Python requests module
Get the EDINET code list in Python
Results that did not get caught in the search with this word
About the case that the speaker did not make sound in Ubuntu LTS 20.04
[Python] Sort the list of pathlib.Path in natural sort
Make a copy of the list in Python
[Mac] Run the RealSense D415 sample in Python
The story of reading HSPICE data in Python
The one that displays the progress bar in Python
The story that fits in with pip installation
Flatten an irregular 2D standard list in Python
Examples and solutions that the Python version specified in pyenv does not run
Sorted list in Python
About the matter that the contents of Python print are not visible in docker logs
Key input that does not wait for key input in Python
Filter List in Python
[python] Get the list of classes defined in the module
The story of FileNotFound in Python open () mode ='w'
Find the part that is 575 from Wikipedia in Python
Not being aware of the contents of the data in python
List find in Python
[Python] Outputs all combinations of elements in the list
Modules that may go through the shell in Python
[Python] How to output the list values in order
How to find the first element that matches your criteria in a Python list
When writing to a csv file with python, a story that I made a mistake and did not meet the delivery date
A story that pyenv is stuck because the python execution command PATH does not pass
Macro (VBA) that converts the contents selected in the range in Excel into the list initialization code in Python.
Make sure all the elements in the list are the same in Python
Test.py is not reflected on the web server in Python3.
[Small story] [Python] Replace strings in 2D arrays with numbers
Sort and output the elements in the list as elements and multiples in Python.
How to get the last (last) value in a list in Python
The story that Python stopped working with VS Code (Windows 10)
A story that I did not know how to load a mixin when making a front with the django app [Beginners learn python with a reference book in one hand]
Download the file in Python
Python list is not a list
Solve ABC175 D in Python
Methods available in the list
Getting list elements in Python
If you think that the person you put in with pip doesn't work → Maybe you are using python3?
The story of creating a bot that displays active members in a specific channel of slack with python
Play a sound in Python assuming that the keyboard is a piano keyboard
python Note: Determine if command line arguments are in the list
Movement that changes direction in the coordinate system I tried Python 3
A story that struggled to handle the Python package of PocketSphinx
New Python grammar and features not mentioned in the introductory book
The story that Pathlib could not access the folder containing the half-width space
How to judge that the cross key is input in Python3
33 strings that should not be used as variable names in python
A function that measures the processing time of a method in python