'How can I make a for statement work when comparing two apended lists? [closed]
The last for statement does not work. I am comparing the outputs of two functions (which are each being appended as a list). Essentially, if the output of the first item in the list for function A is larger than the first item in the list for function B then I would like to print a 1, if it is less then I would like to print 0, otherwise 0.5. However, when I run the code I get 1 for each value compares across both lists. This is false because I've manually checked each value (there should be 1s and 0s and no 0.5s).
from IPython.lib.display import IFrame
#Exptected Value
import numpy as np
import math
natural_log = np.log(1)
Outcomes = [[20, 0],[ 20, 0],[ 20, 0],[ 20, 0],[ 20, 0]]
Probabilities = [[0.001, 0.999],[ 0.01, 0.99],[ 0.1, 0.9],[ 0.25, 0.75],[ 0.5, 0.5]]
Y = []
PT= []
for i in range(0,len(Outcomes)):
Y.append(Outcomes[i][0]*Probabilities[i][0] + Outcomes[i][1]*Probabilities[i][1])
for i in range(0,len(Outcomes)):
PT.append((math.exp(-(-np.log(Probabilities[i][0]))**0.5))*(Outcomes[i][0]**0.5)+(math.exp(-(-np.log(Probabilities[i][1]))**0.5))*(Outcomes[i][1]**0.5))
for i in range(0,len(Outcomes)):
if PT > Y:
print("gamble",i+1,"PT output = 1")
if PT < Y:
print("gamble",i+1,"PT output = 0")
if PT ==Y:
print("gamble",i+1,"PT output = 0.5")
print(PT)
print(Y)
Solution 1:[1]
You need indexing to access each element in a for loop. Otherwise, only the first elements of the list are compared. And when comparing the same value multiple times, it is helpful to use elif and else to improve the processing speed of the program.
for i in range(0,len(Outcomes)):
if PT[i] > Y[i]:
print("gamble",i+1,"PT output = 1")
elif PT[i] < Y[i]:
print("gamble",i+1,"PT output = 0")
else:
print("gamble",i+1,"PT output = 0.5")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Desty |
