np.subtract() - ValueError 用 numpy 减去列表
np.subtract() - ValueError subtracting lists with numpy
我给了不同的组循环。
put_dict
和 incomes_dict
包含每个组的整数列表。
因为 incomes_dict
总是包含更多的值,所以我试图使它们的长度相同。
目的是减去值。
例如:
put_dict[g] = [ 2,3,4]
incomes_dict[g] = [1,2,1]
desired_result[g] = [1,1,3]
代码:
import numpy as np
times = []
for g in GROUPS:
laenge = len(put_dict[g])
times += np.subtract(put_dict[g], incomes_dict[g][:laenge])
错误:
ValueError: operands could not be broadcast together with shapes (0,) (2535,)
这是因为您正试图将一个 numpy 数组与一个 python 列表相加,python 无法理解。
我猜您想将 np.subtract( ... )
的所有元素附加到 times
数组,您可以通过将 np.subtract( ... )
对象转换为 python 列表来实现.
import numpy as np
times = []
for g in GROUPS:
laenge = len(put_dict[g])
times += np.subtract(put_dict[g], incomes_dict[g][:laenge]).tolist()
我给了不同的组循环。
put_dict
和 incomes_dict
包含每个组的整数列表。
因为 incomes_dict
总是包含更多的值,所以我试图使它们的长度相同。
目的是减去值。
例如:
put_dict[g] = [ 2,3,4]
incomes_dict[g] = [1,2,1]
desired_result[g] = [1,1,3]
代码:
import numpy as np
times = []
for g in GROUPS:
laenge = len(put_dict[g])
times += np.subtract(put_dict[g], incomes_dict[g][:laenge])
错误:
ValueError: operands could not be broadcast together with shapes (0,) (2535,)
这是因为您正试图将一个 numpy 数组与一个 python 列表相加,python 无法理解。
我猜您想将 np.subtract( ... )
的所有元素附加到 times
数组,您可以通过将 np.subtract( ... )
对象转换为 python 列表来实现.
import numpy as np
times = []
for g in GROUPS:
laenge = len(put_dict[g])
times += np.subtract(put_dict[g], incomes_dict[g][:laenge]).tolist()