Reputation: 531
I am forced to use python for a part of my code. I coded in ampl my model but now I need to use python to find max of an array.
Here is what I have in AMPL I am wondering can I have kind of the same indices in python?
I have some information from household and members. For each person in each family I have some utility and I want to find the maximum of that.
I have the parameter U on the set E, for utility
set E ,within F cross N;
param U{E};
So U have 2 indices U[f,i], F shows family and i determine the person.
for example in 4-th family, second person has these utilities:
U[4,2]=5
U[4,2]=6
U[4,2]=7
and in 3-rd household, first person has these utility
U[3,1]=8
U[3,1]=9
U[3,1]=1
lets show the max utility by MU, so as an output we have
MU[4,2]=7
MU[3,1]=9
is there any way to find this MU in python?
Upvotes: 0
Views: 740
Reputation: 605
You can use dictionary
in python
to accomplish this. Utility value can be represented as list
of utility values that belong to one specific family and a person. Family and a person can be represented as keys
to the dictionary.
from collections import defaultdict
df = defaultdict(list)
df[4, 2] = [5, 6, 7]
df[3, 1] = [8, 9, 1]
for key in df.keys():
maximum = 0
for value in df[key]:
if value > maximum:
maximum = value
max_keys = key
print("(Family, Person) = ", key, "Max of Utility = ", maximum)
The output of the code will be
(Family, Person) = (4, 2) Max of Utility = 7
(Family, Person) = (3, 1) Max of Utility = 9
Upvotes: 1