[Small story] [Python] Replace strings in 2D arrays with numbers

A note on how to replace a string stored in a Python two-dimensional array with a number. For example, in standard input, numbers are passed as follows.

3
1 2
3 4
5 6

The first line is the number of repetitions of standard input for the transition to the second line. So, I want to store the numerical value of the transition to the second line in a two-dimensional array.

arr = []
n = int(input())
for i in range(n):
    arr.append(input().split())
print(arr)
>>>[['1', '2'], ['3', '4'], ['5', '6']]

At this time, if input is received by input (). Split (), it will be input as a list of character strings. I want to convert this to an int.

When converting with for

for i in range(len(arr)):
    for j in range(len(arr[i])):
        arr[i][j] = int(arr[i][j])
print(arr)
>>>[[1, 2], [3, 4], [5, 6]]

When converting in list comprehension display

arr = [[int(x) for x in y] for y in arr]
print(arr)
>>>[[1, 2], [3, 4], [5, 6]]

Bonus: When making a one-dimensional array

arr = [int(x) for y in arr for x in y]
print(arr)
>>>[1, 2, 3, 4, 5, 6]

Recommended Posts

[Small story] [Python] Replace strings in 2D arrays with numbers
Testing with random numbers in Python
[Small story] Get timestamp with Python
Replace non-ASCII with regular expressions in Python
Bulk replacement of strings in Python arrays
[Small story] Test image generation with Python / OpenCV
python small story collection
Compare strings in Python
Reverse strings in Python
Prime numbers in Python
A story about making 3D space recognition with Python
[Small story] In Python, i = i + 1 is slightly faster than i + = 1.
[Small story] Synchronize with Python without multithreading (limited use)
[Python] Get the numbers in the graph image with OCR
Scraping with selenium in Python
Working with LibreOffice in Python
Scraping with chromedriver in python
Debugging with pdb in Python
Create 3d gif with python3
Search for strings in Python
Determine prime numbers with python
Working with sounds in Python
Scraping with Selenium in Python
python> Handling of 2D arrays
Stumble story with Python array
Solve ABC175 D in Python
Scraping with Tor in Python
Tweet with image in Python
Combined with permutations in Python
Handle complex numbers in Python
The story that 2D list replacement did not work in python
How to display legend marks in one with Python 2D plot
Number recognition in images with Python
Playing handwritten numbers with python Part 1
3D skeleton structure analysis with Python
GOTO in Python with Sublime Text 3
Replace dictionary value with Python> update ()
Scraping with Selenium in Python (Basic)
CSS parsing with cssutils in Python
Solve ABC166 A ~ D with Python
Numer0n with items made in Python
Open UTF-8 with BOM in Python
Use rospy with virtualenv in Python3
Use Python in pyenv with NeoVim
[Beginner] Extract character strings with Python
Heatmap with Dendrogram in Python + matplotlib
Read files in parallel with Python
3D drawing with SceneKit in Pythonista
Password generation in texto with python
Law of large numbers in python
Use OpenCV with Python 3 in Window
Until dealing with python in Atom
Get started with Python in Blender
Working with DICOM images in Python
Extract strings from files in Python
[Small story] How to save matplotlib graphs in a batch with Jupyter
(Small story) Sort columns by column name with one liner in pandas.DataFrame
Write documentation in Sphinx with Python Livereload
Get additional data in LDAP with python
[Python, Julia] 3D display in Jupyter-Mayavi library
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)