我目前正在尝试同时遍历三个列表:
list_weight = [0.9,0.3,0.6,0.4]
list_reliability = [0.8,0.5,0.2,0.8]
belief_CRED0 = [create_belief_matrix ('ACBA').iloc[0]]
belief_CRED0
Out[40]:
[1 0.562500
2 0.562500
3 0.391304
4 0.391304
Name: CRED0, dtype: float64]
首先我创建了一个嵌套循环:
for belief in belief_CRED0:
for weight in list_weight:
for reliability in list_reliability:
m = [(1/(1+weight-reliability))*(weight*belief)]
print(m)
但结果完全没了。所以我尝试这样做:
for belief, weight, reliability in zip(belief_CRED0, list_weight, list_reliability):
m = [(1/(1+weight-reliability))*(weight*belief)]
print(m)
但结果也是错误的:
m
Out[42]:
[1 0.460227
2 0.460227
3 0.320158
4 0.320158
Name: CRED0, dtype: float64]
从出现的结果来看,循环似乎只使用相应列表中的第一个权重和可靠性(权重= 0.9,可靠性= 0.8)。
正确的输出应该是:
[1 0.460227
2 0.210937
3 0.16770171
4 0.26086933
我该怎么办?
答案 0 :(得分:2)
你的for循环中的小错误zip
(这是BTW的最佳方式)。累积结果......而不是保持分配给m
。
m = []
for belief, weight, reliability in zip(belief_CRED0, list_weight, list_reliability):
m.append(weight*belief/(1+weight-reliability))
print(m)
答案 1 :(得分:1)
如果它们都是pandas.Series
或numpy.array
,那么您可以直接执行此操作,例如:
>>> weight = pd.Series(list_weight, index=range(1, 5))
>>> reliability = pd.Series(list_reliability, index=range(1, 5))
>>> 1/(1+weight-reliability)*(weight*belief_CRED0)
1 0.460227
2 0.210937
3 0.167702
4 0.260869
dtype: float64
与numpy
类似:
>>> weight = np.array(list_weight)
>>> reliability = np.array(list_reliability)
>>> 1/(1+weight-reliability)*(weight*belief_CRED0)
1 0.460227
2 0.210937
3 0.167702
4 0.260869
Name: CRED0, dtype: float64