【发布时间】:2022-01-09 13:05:21
【问题描述】:
我有两个具有相同维度的 numpy 数组:权重和百分比。百分比是“真实”数据,权重是直方图中每个“真实”数据的数量。
例如)
weights = [[0, 1, 1, 4, 2]
[0, 1, 0, 3, 5]]
percents = [[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]]
(每一行百分比都是一样的)
我想将这些“相乘”在一起,从而产生 weights[x] * [percents[x]]:
results = [[0 * [1] + 1 * [2] + 1 * [3] + 4 * [4] + 2 * [5]
[0 * [1] + 1 * [2] + 0 * [3] + 3 * [4] + 5 * [5]]
= [[2, 3, 4, 4, 4, 4, 5, 5]
[2, 4, 4, 4, 5, 5, 5, 5, 5]]
请注意,每行的长度可能不同。理想情况下,这可以在 numpy 中完成,但正因为如此,它可能最终成为列表列表。
编辑: 我已经能够将这些嵌套的 for 循环拼凑在一起,但显然这并不理想:
list_of_hists = []
for index in df.index:
hist = []
# Create a list of lists, later to be flattened to 'results'
for i, percent in enumerate(percents):
hist.append(
# For each percent, create a list of [percent] * weight
[percent]
* int(
df.iloc[index].values[i]
)
)
# flatten the list of lists in hist
results = [val for list_ in hist for val in list_]
list_of_hists.append(results)
【问题讨论】:
-
你实际上不需要循环。但由于您输入的数组长度不平衡,因此
np.split之类的内容可能是一个不错的选择。
标签: python list numpy nested-lists