Python hand play (two-dimensional list)

What is this article?

Let's take a note again. I made a 3x3 2D list.

# Like this
print(tmp)
 [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

So, when I tried to put "5" in the middle, it became like this. Why?

# Like this
tmp[1][1] = 5
print(tmp)
 [[0, 5, 0], [0, 5, 0], [0, 5, 0]]

What that···

Trends and countermeasures

It was because I didn't think deeply when I made it.

# Bad example
tmp_ng =[[0] * 3] * 3
print(tmp_ng)
 [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

Alright, OK! (* Not OK ...)

# Improvement example
tmp_ok = [[0] * 3 for _ in range(3)]
print(tmp_ok)
 [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

It doesn't change at this point, but ... It will be like this.

tmp_ng[1][1] = 5
print(tmp_ng)
 [[0, 5, 0], [0, 5, 0], [0, 5, 0]]
tmp_ok[1][1] = 5
print(tmp_ok)
 [[0, 0, 0], [0, 5, 0], [0, 0, 0]]

Reveal (?)

>>> tmp_ok[0] == tmp_ok[2]
True
>>> tmp_ng[0] == tmp_ng[2]
True
>>> tmp_ok[0] is tmp_ok[2]
False
>>> tmp_ng[0] is tmp_ng[2]
True

In a bad example, the object (?) [0] * 3 created by new (in C #) is set in 3 places. In other words, an array is created in which the variable is defined once and the memory allocated location is referenced three times. Therefore, one type of value appears in three places. However, in the improvement example, [0] * 3 is new (in C #) every time the for _ in range (3) is turned 3 times. Since the memory is secured 3 times, each can have a different value.

I see. .. ..

You can't say that you can write Python unless you can naturally recognize the meaning of each character literal, especially the control command.

Recommended Posts

Python hand play (two-dimensional list)
Python hand play (division)
[Python] list
Python hand play (argparse minimum code)
Python hand play (Pandas / DataFrame beginning)
Python basics: list
Python hand play (calculated full of mordred)
Python hand play (descriptor calculation: serious version)
Play Python async
Play with 2016-Python
Python> Comprehension / Comprehension> List comprehension
Python list manipulation
Python hand play (let's get started with AtCoder?)
Python hand play (one line notation of if)
Python hand play (interoperability between CSV and PostgreSQL)
Sorted list in Python
Python Exercise 2 --List Comprehension
List of python modules
Python> list> extend () or + =
Python list comprehension speed
Filter List in Python
python unittest assertXXX list
Play youtube in python
Python3 List / dictionary memo
[Memo] Python3 list sort
OpenCV3 Python API list
Python error list (Japanese)
List find in Python
Python exception class list
Initialize list with python
Python hand play (get column names from CSV file)
Continuously play the first Python Sukusta MV-Scrolling the song list-
Python list, for statement, dictionary
Summary of Python3 list operations
[Python] Convert list to Pandas [Pandas]
[Python] How to use list 1
Create ToDo List [Python Django]
Specify multiple list indexes (Python)
Python Basic Course (5 List Tuples)
Python list is not a list
[Python] Play with Discord's Webhook.
Play RocketChat with API / Python
[Python] Copy of multidimensional list
[Introduction to Python] <list> [edit: 2020/02/22]
Python list and tuples and commas
Paiza Python Primer 4: List Basics
Python list comprehensions and generators
[Python / PyQ] 4. list, for statement
Python #list for super beginners
Getting list elements in Python
Python hand play (RDKit descriptor calculation: SDF to CSV using Pandas)
Extract multiple list duplicates in Python
Difference between list () and [] in Python
[python] Manage functions in a list
Output 2017 Premium Friday list in Python
Convert list to DataFrame with python
Let's play with Excel with Python [Beginner]
[Python beginner] Divide one list (5 lines).
python / Make a dict from a list.
Python> list> Convert double list to single list
[Python] How to use list 3 Added