同时循环三个列表:嵌套循环不起作用
Loop over three lists simultaneously: nested loop not working
我目前正在尝试同时遍历三个列表:
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
我该怎么办?
zip
循环中的小错误(顺便说一句,这是最好的方法)。累加结果...而不是一直分配给 m
.
m = []
for belief, weight, reliability in zip(belief_CRED0, list_weight, list_reliability):
m.append(weight*belief/(1+weight-reliability))
print(m)
如果他们都是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
我目前正在尝试同时遍历三个列表:
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
我该怎么办?
zip
循环中的小错误(顺便说一句,这是最好的方法)。累加结果...而不是一直分配给 m
.
m = []
for belief, weight, reliability in zip(belief_CRED0, list_weight, list_reliability):
m.append(weight*belief/(1+weight-reliability))
print(m)
如果他们都是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