Home > OS >  How to use zip() function to equally distribute the ndarray shape?
How to use zip() function to equally distribute the ndarray shape?

Time:08-11

print('pre_list--->',len(pre_list))  #Output>>36
print('pre_list type--->',type(pre_list))  #Output>><class 'list'> (list of array)
print('label_list--->',len(label_list))  #Output>>36
print('label_list type--->',type(label_list))  #Output>><class 'list'> (list of array)


for m,n in zip(pre_list, label_list):

  print('m shape',m.shape) #Output>> m shape (256, 256)
  print('m length', len(m)) #Output>> 256
  print('n shape', n.shape) #Output>> n shape (4, 256, 256)
  print('n length', len(n)) #Output>> 4

Edit: When I tried to print m and n. This is what it looks like:

m---> [[0 1 1 ... 1 1 0]
 [1 1 1 ... 1 1 1]
 [1 1 1 ... 1 1 1]
 ...
 [1 1 1 ... 1 1 1]
 [1 1 1 ... 1 1 1]
 [0 1 1 ... 1 1 0]]

n---> [[[1 1 1 ... 0 0 1]
  [1 1 1 ... 0 1 1]
  [1 1 1 ... 0 1 1]
  ...
  [1 1 1 ... 0 0 1]
  [1 1 1 ... 0 1 1]
  [1 1 1 ... 1 1 1]]

 [[1 1 1 ... 0 0 1]
  [1 1 1 ... 0 1 1]
  [1 1 1 ... 0 1 1]
  ...
  [1 1 1 ... 0 0 1]
  [1 1 1 ... 0 1 1]
  [1 1 1 ... 1 1 1]]

 [[1 1 1 ... 0 0 1]
  [1 1 1 ... 0 1 1]
  [1 1 1 ... 0 1 1]
  ...
  [1 1 1 ... 0 0 1]
  [1 1 1 ... 0 1 1]
  [1 1 1 ... 1 1 1]]

 [[1 1 1 ... 0 0 1]
  [1 1 1 ... 0 1 1]
  [1 1 1 ... 0 1 1]
  ...
  [1 1 1 ... 0 0 1]
  [1 1 1 ... 0 1 1]
  [1 1 1 ... 1 1 1]]]

I want n.shape to be (256,256) just like m shape. As I'm passing the list of the same length it should be same. Can anyone point out where I made a mistake?

Thanks for the help

CodePudding user response:

zip produces an iterator through the pairs of elements of iterables.

Your code is equivalent to this:

for i in range(len(pre_list)):
  m = pre_list[i]
  n = label_list[i]
  print('m shape', m.shape)
  print('n shape', n.shape)

For example, during the first iteration:

m is the first element of pre_list, and n is the first element of label_list.

m and n have different shapes.


Your pre_list and label_list are lists that contain 36 numpy arrays each. The arrays of pre_list have shapes (256, 256), and the arrays of label_list have shapes (4, 256, 256).

If you want to see the shapes of lists pre_list and label_list themselves, you can convert them to numpy arrays. But they won't be equal because the shapes of the numpy arrays that they contain are not equal.

print(np.array(pre_list).shape)  # (36, 256, 256)
print(np.array(label_list).shape)  # (36, 4, 256, 256)
  • Related