Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

get diagonals of 2d array

test = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]

max_col = len(test[0])
max_row = len(test)
cols = [[] for _ in range(max_col)]
rows = [[] for _ in range(max_row)]
fdiag = [[] for _ in range(max_row + max_col - 1)]
bdiag = [[] for _ in range(len(fdiag))]
min_bdiag = -max_row + 1

for x in range(max_col):
    for y in range(max_row):
        cols[x].append(test[y][x])
        rows[y].append(test[y][x])
        fdiag[x+y].append(test[y][x])
        bdiag[x-y-min_bdiag].append(test[y][x])

print(cols)
print(rows)
print(fdiag)
print(bdiag)
Comment

PREVIOUS NEXT
Code Example
Python :: insert value in string python 
Python :: counting unique values python 
Python :: find difference between two pandas dataframes 
Python :: pipeline model coefficients 
Python :: enum python print all options 
Python :: check if a string is palindrome or not using two pointer function in python 
Python :: make django admin page text box smaller 
Python :: pd calculations between columns 
Python :: python emoji convert 
Python :: how to add items to a set in python 
Python :: display column names as a dictionary pandas 
Python :: beautifulsoup remove tag with class 
Python :: find in python 
Python :: re.search 
Python :: python overwrite multiline text 
Python :: Acticating virtual environment 
Python :: using python for rest api 
Python :: python selenium teardown class 
Python :: rename files in python 
Python :: pytest for loop 
Python :: mistborn order to read 
Python :: Using python-poppler 
Python :: python sh command 
Python :: functools python install 
Python :: using csv module how to read perticular lines in csv 
Python :: get_or_create in django 
Python :: get variable from function python 
Python :: how to use replace in python 
Python :: if-else Conditional Statement in Python 
Python :: python string is in list 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =