Reputation: 780
In the following code:
states = ["A","B","C","D"]
priors = {"A": 1.0, "B": -1.0, "C": 0, "D": 0}
values = {} #setup an empty dictionary
for s in states:
values[s] = {"Value1": priors[s], "Value2": 3.0}
max_val = max(values[prevS]["Value1"]*-1 for prevS in states)
I need to return max_val and the prevS which corresponds to that max value. How can I do this?
Upvotes: 0
Views: 273
Reputation: 1140
Maybe this is that you want?
states = ["A","B","C","D"]
priors = {"A": 1.0, "B": -1.0, "C": 0, "D": 0}
values = ((item[0], item[1]*-1) for item in priors.items() if item[0] in states)
print max(values, key=lambda i:i[1])
Result
('B', 1.0)
Or this:
states = ["A","B","C","D"]
priors = {"A": 1.0, "B": -1.0, "C": 0, "D": 0}
values = {item[0]:{'Value1':item[1]*-1, 'Value2':3.0} for item in priors.items() if item[0] in states}
print max(values.items(), key=lambda i: i[1]['Value1'])
Result
('B', {'Value2': 3.0, 'Value1': 1.0})
Upvotes: 1
Reputation: 251398
You can do this:
>>> max((values[prevS]["Value1"]*-1, prevS) for prevS in states)
(1.0, 'B')
This makes a tuple of the value and its prevS value and takes the max of those. Since the "value" comes first the max will operate on that first, and the prevS value will only be used to break ties.
Upvotes: 2