Home > Back-end >  Output of array is as list not as 1D array in python
Output of array is as list not as 1D array in python

Time:10-15

I am trying to write down a very simple lines to have an 1D array, however, the output is not as expected, it's correct but with word array in front of each 5 elements, I attached the code:

import numpy as np


 c=np.array([[1,11,21,31,5],[4,14,24,34,5],
             [7,17,27,37,5],[31,41,51,61,5],[34,44,54,64,5],
             [37,47,57,67,5],[61,71,81,91,5],[64,74,84,94,5], 
            [64,74,84,94,5],[64,74,84,94,5], 
              [64,74,84,94,5]
             ,[64,74,84,94,5],[64,74,84,94,5],[64,74,84,94,5], 
               [64,74,84,94,5], 
              [67,77,87,97,5]])
  s=np.array([4,3,2,1])
  r_max=np.max(s)
  d  =    []
  x=4
  for I in range(x-1,-2,-2):
      for J in range(r_max-s[I]):
          d=[d,c[r_max*I-J 3]]
    
  print(d)
  # Output: [[[[[[[[], array([67, 77, 87, 97,  5])], array([64, 74, 84, 94,  5])], array([64, 
            # 74, 84, 94,  5])], array([64, 74, 84, 94,  5])], array([67, 77, 87, 97,  5])], 
             # array([64, 74, 84, 94,  5])], array([64, 74, 84, 94,  5])]
  # expected output same numbers but as 1D array as the following:
    #[67, 77, 87, 97, 64, 74, 84, 94,  5, 64, 74, 84, 94,  5, 64, 74, 84, 94,  5,67, 77, 87, 
    # 97,  5,64, 74, 84, 94,  5,64, 74, 84, 94,  5]]

CodePudding user response:

d=[d,c[r_max*I-J 3]], here you're adding a list as an element.

If you want to merge 2 list, you can do: d = d list(c[r_max*I-J 3]]).

You can also use np.concatenate: d = np.concatenate((d, c[r_max*I-J 3]]) (Dont forget to modify the d definition with d = np.array([])

CodePudding user response:

Use list.append instead:

In [373]:   d  =    []
     ...:   x=4
     ...:   for I in range(x-1,-2,-2):
     ...:       for J in range(r_max-s[I]):
     ...:           d.append(c[r_max*I-J 3])
     ...: 
In [374]: d
Out[374]: 
[array([67, 77, 87, 97,  5]),
 array([64, 74, 84, 94,  5]),
 array([64, 74, 84, 94,  5]),
 array([64, 74, 84, 94,  5]),
 array([67, 77, 87, 97,  5]),
 array([64, 74, 84, 94,  5]),
 array([64, 74, 84, 94,  5])]
In [375]: np.array(d)
Out[375]: 
array([[67, 77, 87, 97,  5],
       [64, 74, 84, 94,  5],
       [64, 74, 84, 94,  5],
       [64, 74, 84, 94,  5],
       [67, 77, 87, 97,  5],
       [64, 74, 84, 94,  5],
       [64, 74, 84, 94,  5]])

When you collect numpy arrays in a list, the display includes the word array. It's telling us that the list contains arrays. The elements of the d list are all the same shape, so np.array can produce a nice 2d numeric array.

With d=[d,c[r_max*I-J 3]] you have collected the same arrays, but in a deeply nested list of lists. That cannot be turned into a multidimensional array.

When printing objects, python does not throw in random words like 'array' or brackets. Those are produced by the objects themselves, and tell something about their identity and structure. Learn to read that information. Don't skimp on the basic python and numpy reading.

1d expectation

That 2d array can be turned into a 1d one with ravel:

In [376]: np.array(d).ravel()
Out[376]: 
array([67, 77, 87, 97,  5, 64, 74, 84, 94,  5, 64, 74, 84, 94,  5, 64, 74,
       84, 94,  5, 67, 77, 87, 97,  5, 64, 74, 84, 94,  5, 64, 74, 84, 94,
        5])

Another option is to use extend when collecting the arrays in d:

In [377]:   d  =    []
     ...:   x=4
     ...:   for I in range(x-1,-2,-2):
     ...:       for J in range(r_max-s[I]):
     ...:           d.extend(c[r_max*I-J 3])
     ...: 
In [378]: d
Out[378]: 
[67,
 77,
 87,
 ...
 94,
 5]
In [379]: np.array(d)
Out[379]: 
array([67, 77, 87, 97,  5, 64, 74, 84, 94,  5, 64, 74, 84, 94,  5, 64, 74,
       84, 94,  5, 67, 77, 87, 97,  5, 64, 74, 84, 94,  5, 64, 74, 84, 94,
        5])

hstack could also be used with the original d.

  • Related