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)]
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
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
https://note.nkmk.me/python-list-transpose/ I referred to the familiar nkmk's blog.
Recommended Posts