Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Semrush - keyword research tool

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Teach Your Child To Read

Disclosure: My content contains affiliate links.

31,647 questions

41,700 answers

573 users

How to print the entire row and column of a specific cell in a matrix with Python

2 Answers

0 votes
import numpy as np
 
matrix = np.array([ [1, 2, 3, 4, 5],
                    [0, 3, 6, 7, 8],
                    [9, 9, 6, 5, 4],
                    [8, 8, 8, 1, 1],
                    [2, 1, 1, 3, 5] ])
           
cell_row = 2
cell_col = 3
 
print(matrix[cell_row,: ])
print(matrix[:, cell_col])
 
 
 
'''
run:
 
[9 9 6 5 4]
[4 7 5 1 3]
 
'''

 



Learn & Practice Python
with the most comprehensive set of 13 hands-on online Python courses
Start now


answered Jul 9 by avibootz
edited Jul 9 by avibootz
0 votes
def getColumn(matrix, i):
    return [row[i] for row in matrix]

matrix = [ [1, 2, 3, 4, 5],
           [0, 3, 6, 7, 8],
           [9, 9, 6, 5, 4],
           [8, 8, 8, 1, 1],
           [2, 1, 1, 3, 5] ]
          
cell_row = 2
cell_col = 3

print(matrix[cell_row])
print(getColumn(matrix, cell_col))



'''
run:

[9 9 6 5 4]
[4 7 5 1 3]

'''

 



Learn & Practice Python
with the most comprehensive set of 13 hands-on online Python courses
Start now


answered Jul 9 by avibootz
...