How to extract all K*K submatrix of matrix with or without NumPy?

2024/10/5 15:42:01

This is my input:

row=6 col=9

6
9
s b k g s y w g f
r g y e q j j a s
s m s a s z s l e
u s q u e h s s s
g s f h s s e s g
x d r h g y s s s

This is my code:

r=int(input())
c=int(input())
n=min(r,c)
k=3
matrix=[list(map(str,input().split())) for i in range(r)]
t = []
for i in range(0,n-1,k):for j in range(0,n-1,k):t.append([matrix[i+ii][j+jj] for ii in range(k) for jj in range(k)])
print(t) 

output got:

[['s', 'b', 'k', 'r', 'g', 'y', 's', 'm', 's'], ['g', 's', 'y', 'e', 'q', 'j', 'a', 's', 'z'], ['u', 's', 'q', 'g', 's', 'f', 'x', 'd', 'r'], ['u', 'e', 'h', 'h', 's', 's', 'h', 'g', 'y']]

this code not printing my last 3 columns why? Please help me

Answer
r=int(input())
c=int(input())
n=min(r,c)
k=3
matrix=[list(map(str,input().split())) for i in range(r)]
t = []
a=[]
for i in range(0,r,k):for j in range(0,c,k):t.append([matrix[i+ii][j+jj] for ii in range(k) for jj in range(k)])
print(t) 

In for loop you used n variable in range. so it will end in the minimum of row and col .That's why you got error..Thank you

https://en.xdnf.cn/q/119914.html

Related Q&A

How to scrape multiple result having same tags and class

My code is accurate for single page but when I run this code for multiple records using for loop and if there are some data missing like person then (as I used index no[1] and [2] for person variable ,…

Is there an alternative for sys.exit() in python?

try:x="blaabla"y="nnlfa" if x!=y:sys.exit()else:print("Error!") except Exception:print(Exception)Im not asking about why it is throwing an error. I know that it raises e…

Adding items to Listbox in Python Tkinter

I would like my Listbox widget to be updated upon clicking of a button. However I encountered a logic error. When I click on the button, nothing happens. No errors at all.listOfCompanies: [[1, ], [2, -…

Policy based design in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

Exception raised: cannot import name manual_seed from torch

im trying to run the AutoClean project on my device (heres my code): import random from AutoClean import AutoClean import pandas as pddef __init__(self, pipeline, resultat ):self.pipeline = pipelinesel…

Compile a C/C++ Program and store standard output in a File via Python

Lets say I have a C/C++ file named userfile.c. Using Python, how can I invoke the local gcc compiler so that the file is compiled and an executable is made? More specifically, I would like to provide …

How to swap maximums with the minimums? (python)

Is there a method to swap the maximum and the minimum of a list? The list will be as follows and the program has to be continued so that it will print the maximum swapped with the minimum, the second …

python object attributes and methods

In python all data is object and any object should have attributes and methods. Does somebody know python object without any attributes and methods?>>> len(dir(1)) 64

How to retrieve nested data with BeautifulSoup?

I have the below webpage source: </li><li class="cl-static-search-result" title="BELLO HONDA ACCORD &quot;95 MIL MILLAS&quot;. REALMENTE COMO NUEVO"><a href=&…

applying onehotencoder on numpy array

I am applying OneHotEncoder on numpy array.Heres the codeprint X.shape, test_data.shape #gives 4100, 15) (410, 15) onehotencoder_1 = OneHotEncoder(categorical_features = [0, 3, 4, 5, 6, 8, 9, 11, 12]) …