How to transpose a 2D array using only python [Note]

Thing you want to do

Transpose a two-dimensional array of python and further convert int to str I want to do it without using pandas or nd.array

example.txt


[
[5, 0, 1, 1], 
[1, 0, 1, 5], 
[0, 1, 6, 0], 
[0, 4, 3, 0], 
[5, 2, 0, 0], 
[5, 0, 1, 1], 
[0, 6, 0, 1], 
[0, 1, 0, 6]
]

=>

[
['5', '1', '0', '0', '5', '5', '0', '0'], 
['0', '0', '1', '4', '2', '0', '6', '1'],
 ['1', '1', '6', '3', '0', '1', '0', '0'], 
['1', '5', '0', '0', '0', '1', '1', '6']
]

code

hoge.py


cnt_ACGT_t = [list(map(str,i)) for i in zip(*cnt_ACGT)]

Transpose

Use zip, a built-in function that takes multiple iterators and returns an iterator grouped by elements in the same location

zip(*cnt_ACGT)

Transposed list of cnt_ACGT in was converted to an iterator object

Conversion of all elements from int to str

Since zip (* cnt_ACGT) is an iterator, convert each element to str using the list comprehension as it is. Convert to sql by value for line-by-line list

reference

https://note.nkmk.me/python-list-transpose/ I referred to the familiar nkmk's blog.

Recommended Posts

How to transpose a 2D array using only python [Note]
[Python] How to convert a 2D list to a 1D list
How to make a Python package using VS Code
[Python] How to create a 2D histogram with Matplotlib
How to execute a command using subprocess in Python
How to write a Python class
How to slice a block multiple array from a multiple array in Python
How to convert an array to a dictionary with Python [Application]
[Python] How to swap array values
How to build a LAMP environment using Vagrant and VirtulBox Note
How to make a string into an array or an array into a string in Python
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
How to save only a part of a long video using OpenCV
[Python] How to store a csv file as one-dimensional array data
How to update a Tableau packaged workbook data source using Python
[Python] How to make a class iterable
How to draw a graph using Matplotlib
[Python] How to invert a character string
How to install a package using a repository
How to get a stacktrace in python
Multiply a 3D array using Numpy's einsum
How to run a Maya Python script
How to generate a new loggroup in CloudWatch using python within Lambda
How to get a value from a parameter store in lambda (using python)
How to sort by specifying a column in the Python Numpy array.
How to read a CSV file with Python 2/3
How to create a Python virtual environment (venv)
How to code a drone using image recognition
How to open a web browser from python
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to draw a 3D graph before optimization
How to create a JSON file in Python
How to generate a Python object from JSON
How to add a Python module search path
Python Note: When assigning a value to a string
How to notify a Discord channel in Python
[Note] How to create a Ruby development environment
[Python] How to draw a histogram in Matplotlib
How to upload to a shared drive using pydrive
How to uninstall a module installed using setup.py
[Note] How to create a Mac development environment
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
[python] How to sort by the Nth Mth element of a multidimensional array
How to install Python
How to install python
Suddenly I needed to work on a project using Python and Pyramid, so a note of how I'm studying
Note to daemonize python
How to convert / restore a string with [] in python
How to write a GUI using the maya command
[Python] How to draw a line graph with Matplotlib
(Python) Try to develop a web application using Django
How to auto-submit Microsoft Forms using python (Mac version)
[Python] How to expand variables in a character string
How to write a list / dictionary type of Python3
How to hold a hands-on seminar using Jupyter using docker
How to build a Django (python) environment on docker
Things to note when initializing a list in Python
How to run setUp only once in python unittest
[Python] How to write a docstring that conforms to PEP8
How to exit when using Python in Terminal (Mac)