将列表乘以 ndarrays 列表
Posted
技术标签:
【中文标题】将列表乘以 ndarrays 列表【英文标题】:multiply list by list of ndarrays 【发布时间】:2021-07-30 14:13:04 【问题描述】:我想将一个数字列表乘以一个ndarray,即列表中的每个数字都乘以第一个ndarray。
这是列表:
rad_glob = list(df_radiation_global['Radiacion Global'])
rad_glob
[840.6019233865843,
838.5216723344233,
848.0960914721192,
868.2549621516736,
856.395770948553,
834.7011163700374,
845.9590749318633,
879.446257064898,
650.7904290945771,
814.4754071050489,
736.1105621236436,
736.0329394056781,
873.7764268803811,
870.1957716272296,
904.985074880194,
858.1357154671834,
830.0583941643629,
878.693702287297,
848.669553553067,
863.545204861961,
825.3890093196567,
842.5188280674437,
865.3524589935482,
833.5260196891436,
276.4022647360663,
915.9376255942822,
864.3266555276591,
770.448229104661,
857.8907668737729,
837.7897681015845,
838.6796919844484,
416.3444326406533,
568.653862415028,
884.4158558809625,
825.3940207623805,
798.0613412638792,
863.0410218716003]
这是 ndarray 的列表/df
alb_lo = df_radiation_global['1 - Albedo']
alb_lo
0 [[nan, nan, nan, nan, nan, nan, nan, 0.8528845...
1 [[nan, nan, nan, nan, nan, nan, nan, nan, nan,...
2 [[nan, nan, nan, nan, nan, nan, nan, 0.8528845...
3 [[nan, nan, nan, nan, nan, nan, nan, 0.8528845...
4 [[nan, nan, nan, nan, nan, nan, nan, 0.8528845...
5 [[nan, nan, nan, nan, nan, nan, nan, 0.8528845...
6 [[nan, nan, nan, nan, nan, nan, nan, nan, nan,...
7 [[nan, nan, nan, nan, nan, nan, nan, nan, nan,...
8 [[nan, nan, nan, nan, nan, nan, nan, nan, nan,...
9 [[nan, nan, nan, nan, nan, nan, nan, nan, nan,...
10 [[nan, nan, nan, nan, nan, nan, 0.781219920000...
11 [[nan, nan, nan, nan, nan, nan, nan, 0.8528845...
12 [[nan, nan, nan, nan, nan, nan, nan, nan, nan,...
13 [[nan, nan, nan, nan, nan, nan, nan, nan, nan,...
14 [[nan, 0.79422526, 0.7942568400000001, 0.79905...
15 [[nan, nan, nan, nan, nan, nan, nan, nan, nan,...
16 [[nan, nan, nan, nan, nan, nan, nan, nan, nan,...
17 [[nan, nan, nan, nan, nan, nan, nan, nan, nan,...
18 [[nan, nan, nan, nan, nan, nan, nan, 0.8528845...
19 [[nan, nan, nan, nan, nan, nan, nan, 0.8528845...
20 [[nan, nan, nan, nan, nan, nan, nan, nan, nan,...
21 [[nan, nan, nan, nan, nan, nan, nan, nan, nan,...
22 [[nan, nan, nan, nan, nan, nan, 0.80179372, 0....
23 [[nan, nan, nan, nan, nan, nan, nan, 0.8528845...
24 [[nan, nan, nan, nan, nan, nan, nan, nan, nan,...
25 [[nan, nan, 0.85725789, 0.85735359, 0.86033684...
26 [[nan, nan, nan, nan, nan, nan, nan, nan, nan,...
27 [[nan, nan, nan, nan, nan, nan, nan, 0.8528845...
28 [[nan, nan, nan, nan, nan, nan, nan, 0.8528845...
29 [[nan, nan, nan, nan, nan, 0.84266864, nan, na...
30 [[nan, nan, nan, nan, nan, nan, nan, nan, nan,...
31 [[nan, nan, nan, nan, nan, nan, nan, 0.8528845...
32 [[nan, nan, nan, nan, nan, nan, nan, 0.8528845...
33 [[nan, nan, nan, nan, nan, nan, nan, 0.8528845...
34 [[nan, nan, nan, nan, nan, nan, nan, 0.8528845...
35 [[nan, nan, nan, nan, nan, nan, nan, 0.8528845...
36 [[nan, nan, nan, nan, nan, nan, nan, nan, nan,...
Name: 1 - Albedo, dtype: object
我想将列表中的第一项乘以第一个 ndarray。 nan 值仍然是 nan 并且只是乘以浮点数。我尝试过使用 map、lambda 函数、for 迭代,但没有成功。我经常遇到的错误是:
TypeError: can't multiply sequence by non-int of type 'str'. It can create a separate column with the new data, or simply create a list.
【问题讨论】:
欢迎来到 Stack Overflow!您能否将输入的大小减少到重新创建问题所需的最少行和列数,并包括提供的输入的预期输出?见MRE - Minimal, Reproducible, Examplealb_lo
的形状是什么?
如果你想问这个问题,我已经说得更清楚了***.com/questions/67449920/…
你确定行是ndarray
吗?尝试alb_lo.apply(type)
并检查所有行都是<class 'numpy.ndarray'>
。
【参考方案1】:
您需要做的就是从列表类创建可以处理 nan 元素的新类,这样您就不会遇到异常 尝试: var=[2,2,3]*"无" 例外作为你的例外: print("这是你的错误。让我告诉你如何解决它",yourexception) 类 list_handle_none(list):
def __mul__(self, other):
if type(other)==type(3):
return list(self)*other
elif type(other)==type("nan"):
# raise TypeError("can't multiply sequence by non-int of type 'list'")
def handling_none(a):
if "nan" in[a,other]:
return "nan"
else:
return a*b
return list(map(handling_none,list(self)))
print(list_handle_none([1,2,3]))
print(list_handle_none([1,2,3])*"nan")
【讨论】:
以上是关于将列表乘以 ndarrays 列表的主要内容,如果未能解决你的问题,请参考以下文章
python中numpy.ndarray怎么删除指定下标的元素
Python中ndarray对象和list(列表)的相互转换
numpy 创建ndarray(from existing data)