[Python] I don't understand what I'm doing with table [key] [0] + = 1

I don't know what I'm doing with table [key] [0] + = 1.

While looking at someone else's notebook on Kaggle, I found the following statement:

import random
import string
import collections

action_seq = [2,1]
action_seq, table = [], collections.defaultdict(lambda: [0, 0, 0]) 
key = ''.join([str(a) for a in action_seq[:-1]])
table[key][0] += 1 
print(table[key][0])

The above execution result is as follows.

1

To be honest, I had no idea what would result in a "1". I also checked the processing contents of collections.defaultdict () and lambda, but I still don't understand. In the first place, I couldn't imagine what the result would be if I did "+ = 1" on the array.

How did you understand

I was able to understand how to move by performing the following debugging.

import random
import string
import collections

action_seq, table = [], collections.defaultdict(lambda: [0, 0, 0]) 
action_seq = [2,1]
key = ''.join([str(a) for a in action_seq[:-1]])
print(table)
table[key][0] += 1 
print(table)
table[key][1] += 1 
print(table)
table[key][2] += 1 
print(table) 

The result is as follows.

defaultdict(<function <lambda> at 0x7fd38dfa43b0>, {})
defaultdict(<function <lambda> at 0x7fd38dfa43b0>, {'2': [1, 0, 0]})
defaultdict(<function <lambda> at 0x7fd38dfa43b0>, {'2': [1, 1, 0]})
defaultdict(<function <lambda> at 0x7fd38dfa43b0>, {'2': [1, 1, 1]})

What can be seen from this result is that "table [key] [0] + = 1" is processed with the following configuration.

table[key]...Set table key to "2"
[0]...Set by default[0,0,0]Specify the first column of
+= 1...Add 1 to the value in the first column of the table key 2 array

It turned out that [0] specified after [key] is a column specification.

It took me a long time to understand this process, so I hope it helps.

Recommended Posts

[Python] I don't understand what I'm doing with table [key] [0] + = 1
What I did with a Python array
I don't understand join
I tried fp-growth with python
I tried scraping with Python
I made blackjack with python!
What I did to welcome the Python2 EOL with confidence
I tried gRPC with Python
I tried scraping with python
I understand Python in Japanese!
I made blackjack with Python.
What I learned in Python
I made wordcloud with Python.
What I was addicted to with json.dumps in Python base64 encoding
What should I do with the Python directory structure after all?
[Python] What is a with statement?
I can't install python3 with pyenv-vertualenv
I tried web scraping with python.
[python] What is the sorted key?
I sent an SMS with Python
What to do with PYTHON release?
I liked the tweet with python. ..
I played with PyQt5 and Python3
I want to debug with Python
I tried running prolog with python 3.8.2.
I made a daemon with Python
I tried SMTP communication with Python
Python | What you can do with Python
What skills do I need to program with the FBX SDK Python?
I want to tweet on Twitter with Python, but I'm addicted to it
A reminder of what I got stuck when starting Atcoder with python
[Feature-length poem] I don't understand functional languages You can understand it with Python: Part 1 Functions that receive functions are convenient.
I tried scraping Yahoo News with Python
I made a character counter with Python
I drew a heatmap with seaborn [Python]
I wanted to solve ABC160 with Python
I tried sending an email with python.
Notes on doing Japanese OCR with Python
I want to analyze logs with Python
I want to play with aws with python
What I got from Python Boot Camp
I tried a functional language with Python
I tried recursion with Python ② (Fibonacci sequence)
I installed and used Numba with Python3.5
Question: Multiple integrals with python don't work
I made a Hex map with Python
Excel table creation with Python [Progress management table]
I'm using tox and Python 3.3 with Travis-CI
I made a roguelike game with Python
What I was addicted to Python autorun
I wanted to solve ABC172 with Python
I made a simple blackjack with Python
I made a configuration file with Python
#I tried something like Vlookup with Python # 2
I made a neuron simulator with Python
What I did to save Python memory
What I did when I got stuck in the time limit with lambda python