Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将两个不同数组中的值相加以计算平均值_Python - Fatal编程技术网

Python 将两个不同数组中的值相加以计算平均值

Python 将两个不同数组中的值相加以计算平均值,python,Python,我有两个数组,需要通过将每个数组中的值相加,将它们组合成一个数组 a = [1, 2, 3, 4, 5] b = [1, 2, 3, 4, 5] x = np.sum(a, b) 我想要的结果是: x = [2, 4, 6, 8, 10] 然后,为了能够计算每个值的平均值以获得结果: x = [1, 2, 3, 4, 5] 当我运行它时,它返回错误 TypeError: 'list' object cannot be interpreted as an integer 使用一些好的o

我有两个数组,需要通过将每个数组中的值相加,将它们组合成一个数组

a = [1, 2, 3, 4, 5]
b = [1, 2, 3, 4, 5]

x = np.sum(a, b)
我想要的结果是:

x = [2, 4, 6, 8, 10]
然后,为了能够计算每个值的平均值以获得结果:

x = [1, 2, 3, 4, 5] 
当我运行它时,它返回错误

TypeError: 'list' object cannot be interpreted as an integer

使用一些好的ole列表理解。在这种情况下,
zip
功能也将对我们有所帮助

result = [x+y for x, y in zip(a,b)]
Zip将x的每个元素与y的一个元素在同一索引处连接起来,并在一个列表用完时停止。列表理解获取新创建的列表中的每个元素,并将相邻的两个元素相加

看起来像是这样:

for n,z in zip(x,y):
    x.append(n+z)
例如:

> a = b = [1,2,3,4,5]
> result = [x+y for x, y in zip(a,b)]
> result
[2, 4, 6, 8, 10]

使用一些好的ole列表理解。在这种情况下,
zip
功能也将对我们有所帮助

result = [x+y for x, y in zip(a,b)]
Zip将x的每个元素与y的一个元素在同一索引处连接起来,并在一个列表用完时停止。列表理解获取新创建的列表中的每个元素,并将相邻的两个元素相加

看起来像是这样:

for n,z in zip(x,y):
    x.append(n+z)
例如:

> a = b = [1,2,3,4,5]
> result = [x+y for x, y in zip(a,b)]
> result
[2, 4, 6, 8, 10]

sum
获取一系列对象的总和。例如,
sum(a)
将产生值
15
。你给了它两张单子

由于要添加两个数组,因此需要首先将列表转换为numpy数组;然后可以将它们作为numpy向量相加

>>> import numpy as np
>>> a = np.array([1, 2, 3, 4, 5])
>>> b = np.array([1, 2, 3, 4, 5])
>>> a+b
array([ 2,  4,  6,  8, 10])
>>> (a+b)/2
array([ 1.,  2.,  3.,  4.,  5.])

sum
获取一系列对象的总和。例如,
sum(a)
将产生值
15
。你给了它两张单子

由于要添加两个数组,因此需要首先将列表转换为numpy数组;然后可以将它们作为numpy向量相加

>>> import numpy as np
>>> a = np.array([1, 2, 3, 4, 5])
>>> b = np.array([1, 2, 3, 4, 5])
>>> a+b
array([ 2,  4,  6,  8, 10])
>>> (a+b)/2
array([ 1.,  2.,  3.,  4.,  5.])
请看axis=0示例。请看axis=0示例。