Ask Your Question

Revision history [back]

You should not use a symbolic function for such purpose, symbolic functions should be understood as mathematical expressions, they are usefull if you want to view them as a mathematical object, for example if you want to derive them.

Instead use a Python function, which is an object that returns a value given an entry.

There are two equivalent ways to define a Python function:

sage: def f(x): 
....:     return round(1/add([10,20,30]) * x, 2)

Or, the shorter

sage: f = lambda x : round(1/sum([10,20,30]) * x, 2)

In both cases, you can do:

sage: percent_votes_cand=list(map(f,[10, 20 ,30]))
sage: percent_votes_cand
[0.17, 0.33, 0.5]

You should not use a symbolic function for such purpose, symbolic functions should be understood as mathematical expressions, they are usefull if you want to view them as a mathematical object, for example if you want to derive derivate them.

Instead use a Python function, which is an object that returns a value given an entry.

There are two equivalent ways to define a Python function:

sage: def f(x): 
....:     return round(1/add([10,20,30]) * x, 2)

Or, the shorter

sage: f = lambda x : round(1/sum([10,20,30]) * x, 2)

In both cases, you can do:

sage: percent_votes_cand=list(map(f,[10, 20 ,30]))
sage: percent_votes_cand
[0.17, 0.33, 0.5]