简体   繁体   English

将 numpy.ndarray 隐藏到列表中

[英]Covert numpy.ndarray to a list

I'm trying to convert this numpy.ndarray to a list我正在尝试将此 numpy.ndarray 转换为列表

[[105.53518731]
 [106.45317529]
 [107.37373843]
 [108.00632646]
 [108.56373502]
 [109.28813113]
 [109.75593207]
 [110.57458371]
 [111.47960639]]

I'm using this function to convert it.我正在使用这个 function 来转换它。

conver = conver.tolist()

the output is this, I'm not sure whether it's a list and if so, can I access its elements by doing cover[0], etc output 是这个,我不确定它是否是一个列表,如果是这样,我可以通过覆盖 [0] 等来访问它的元素

[[105.5351873125], [106.45317529411764], [107.37373843478261], [108.00632645652173], [108.56373502040816], [109.28813113157895], [109.75593206666666], [110.57458370833334], [111.47960639393939]]

finally, after I convert it to a list, I try to multiply the list members by 1.05 and get this error!最后,在将其转换为列表后,我尝试将列表成员乘以 1.05 并得到此错误!

TypeError: can't multiply sequence by non-int of type 'float' TypeError:不能将序列乘以“float”类型的非整数

You start with a 2d array, with shape (n,1), like this:您从一个形状为 (n,1) 的二维数组开始,如下所示:

In [342]: arr = np.random.rand(5,1)*100                                                                
In [343]: arr                                                                                          
Out[343]: 
array([[95.39049043],
       [19.09502087],
       [85.45215423],
       [94.77657561],
       [32.7869103 ]])

tolist produces a list - but it contains lists; tolist 生成一个列表 - 但它包含列表; each [] layer denotes a list.每个 [] 层表示一个列表。 Notice that the [] nesting matches the array's:注意 [] 嵌套匹配数组的:

In [344]: arr.tolist()                                                                                 
Out[344]: 
[[95.39049043424225],
 [19.095020872584335],
 [85.4521542296349],
 [94.77657561477125],
 [32.786910295446425]]

To get a number you have to index through each list layer:要获得一个数字,您必须对每个列表层进行索引:

In [345]: arr.tolist()[0]                                                                              
Out[345]: [95.39049043424225]
In [346]: arr.tolist()[0][0]                                                                           
Out[346]: 95.39049043424225
In [347]: arr.tolist()[0][0]*1.05                                                                      
Out[347]: 100.16001495595437

If you first turn the array into a 1d one, the list indexing is simpler:如果您首先将数组转换为一维数组,则列表索引更简单:

In [348]: arr.ravel()                                                                                  
Out[348]: array([95.39049043, 19.09502087, 85.45215423, 94.77657561, 32.7869103 ])
In [349]: arr.ravel().tolist()                                                                         
Out[349]: 
[95.39049043424225,
 19.095020872584335,
 85.4521542296349,
 94.77657561477125,
 32.786910295446425]
In [350]: arr.ravel().tolist()[0]                                                                      
Out[350]: 95.39049043424225

But if your primary goal is to multiply the elements, doing with the array is simpler:但是,如果您的主要目标是乘以元素,则使用数组更简单:

In [351]: arr * 1.05                                                                                   
Out[351]: 
array([[100.16001496],
       [ 20.04977192],
       [ 89.72476194],
       [ 99.5154044 ],
       [ 34.42625581]])

You can access elements of the array with:您可以通过以下方式访问数组的元素:

In [352]: arr[0,0]                                                                                     
Out[352]: 95.39049043424225

But if you do need to iterate, the tolist() option is good to know.但是如果您确实需要迭代,那么tolist()选项很高兴知道。 Iterating on lists is usually faster than iterating on an array.迭代列表通常比迭代数组更快。 With an array you should try to use the fast whole-array methods.对于数组,您应该尝试使用快速的整体数组方法。

you convert to list of list, so you could not broadcast.您转换为列表列表,因此您无法广播。

import numpy as np
x = [[105.53518731],
 [106.45317529],
 [107.37373843],
 [108.00632646],
 [108.56373502],
 [109.28813113],
 [109.75593207],
 [110.57458371],
 [111.47960639],]
x = np.hstack(x)
x * 1.05

array([110.81194668, 111.77583405, 112.74242535, 113.40664278,
       113.99192177, 114.75253769, 115.24372867, 116.1033129 ,
       117.05358671])

yes, it's a list, you can check the type of a variable:是的,它是一个列表,您可以检查变量的类型:

type(a)

to multiply each element with 1.05 then run the code below:将每个元素乘以1.05 ,然后运行以下代码:

x = [float(i[0]) * 1.05 for i in a]
print(x)

Try this:尝试这个:

import numpy as np

a = [[105.53518731],
 [106.45317529],
 [107.37373843],
 [108.00632646],
 [108.56373502],
 [109.28813113],
 [109.75593207],
 [110.57458371],
 [111.47960639]]

b = [elem[0] for elem in a]
b = np.array(b)

print(b*1.05)

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

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