Reputation: 333
I have this variable attributes
that contains lists:
print(attributes)
['glucose_tol', 'age']
['glucose_tol', 'age', 'mass_index']
['glucose_tol', 'age', 'mass_index']
['age']
['age']
['glucose_tol', 'age']
['glucose_tol', 'age']
[]
['glucose_tol', 'age']
['glucose_tol', 'age', 'mass_index']
What I want is to map glucose_tol to 17.61
, mass_index to 1.00
and age to 1.00
and then add these up.
So for example [17.61, 1.00]
will be 18.61
, for ['17.61', '1', '1']
will be 19.61
etc and then add all these up to a number.
I looked at python's map()
function but I don't know how to use it in this case, because I have many lists in one variable.
when I do:
print(type(attributes))
print(attributes)
I get :
<class 'list'>
['glucose_tol', 'age']
<class 'list'>
['glucose_tol', 'age', 'mass_index']
<class 'list'>
['glucose_tol', 'age', 'mass_index']
and so on...
Upvotes: 0
Views: 799
Reputation: 1629
Here is the code you need:
total = 0
mapping = {
”glucose_tol”: 17.61,
”age”: 1.0,
”mass_index”: 1.0
}
for attr in attributes:
for item in attr:
total += mapping[item]
Upvotes: 0
Reputation: 546
please test this:
mapping = {'glucose_tol': 17.61, 'age': 1, 'mass_index': 1}
values = [
['glucose_tol', 'age'],
['glucose_tol', 'age', 'mass_index'],
['glucose_tol', 'age', 'mass_index']
]
for i in values:
print(sum([mapping[y] for y in i]))
# with map
print(sum(list(map(lambda x: mapping[x], i))))
Upvotes: 2