简体   繁体   English

数组的输出是列表而不是python中的一维数组

[英]Output of array is as list not as 1D array in python

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:我试图写下一个非常简单的行来获得一个一维数组,但是,输出并不像预期的那样,它是正确的,但是每个 5 个元素前面都有单词数组,我附上了代码:

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]]

Use list.append instead:使用list.append代替:

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 .当您在列表中收集numpy数组时,显示内容包括单词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. d列表的元素都是相同的形状,因此np.array可以生成一个很好的二维数值数组。

With d=[d,c[r_max*I-J+3]] you have collected the same arrays, but in a deeply nested list of lists.使用d=[d,c[r_max*I-J+3]]您收集了相同的数组,但是在一个深度嵌套的列表列表中。 That cannot be turned into a multidimensional array.那不能变成多维数组。

When printing objects, python does not throw in random words like 'array' or brackets.打印对象时,python 不会抛出诸如“数组”或括号之类的随机单词。 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.不要吝啬基本的python和numpy阅读。

1d expectation 1d 期望

That 2d array can be turned into a 1d one with ravel :可以使用ravel将二维数组变成一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 :另一种选择是在收集d的数组时使用extend

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 . hstack也可以与原始d一起使用。

d=[d,c[r_max*I-J+3]] , here you're adding a list as an element. d=[d,c[r_max*I-J+3]] ,在这里你添加一个列表作为一个元素。

If you want to merge 2 list, you can do: d = d + list(c[r_max*I-J+3]]) .如果要merge 2 个列表,可以执行以下操作: 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([])您也可以使用np.concatenated = np.concatenate((d, c[r_max*I-J+3]]) (不要忘记用d = np.array([])修改d定义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM