0

In Python I want to make two line for-loop. Code is

data = [[1,2,3,4], [5,6,7,8], [9,10,11,12],[13,14,15,16]]
a = np.array(data)
for i in range(0,len(b),3):
for j in range(0,len(b[0]),3):
    sam = [row[j:j+3] for row in b[i:i+3]]         # The problem code
    # result : [array([1, 2, 3]), array([5, 6, 7]), array([ 9, 10, 11])]

that problem code is one line for-loop. I tried this to two-line for-loop.

for row in b[i:i+3]:
        sam = [row[j:j+3]]
# result : [array([1, 2, 3])]
# result : [array([5, 6, 7])]
# result : [array([ 9, 10, 11])]
# i want : [array([1, 2, 3]), array([5, 6, 7]), array([ 9, 10, 11])]

It seems to have been executed properly, but the for-loop in one sentence has all the values in one list, and the for-loop in two lines has separate values. Which part should I fix?

skool high
  • 23
  • 4

0 Answers0