I'm trying to perform separate mathematical operations on a python list. As an example:
[1000, 0.15, 1150, 0.1, 0.2, 828, 49.68, 877.68, 100, 977.68]
I would like to multiply 1000 by 0.15, then take that value and add it to 1000 (result = 1150).
I would then like to carry out other mathematical operations (division, multiplication, addition etc) on the 1150, using values in the same list.
What would be the best general approach here?
I'm not looking for anybody to write me all the code but would numpy be the best approach? Can I achieve this using for loops?
解决方案
You can do this just by indexing the position in the list...
lst = [1000, 0.15, 1150, 0.1, 0.2, 828, 49.68, 877.68, 100, 977.68]
val = lst[0]
val += (val*lst[1])
print(val)
>>> 1150
print(val=lst[2])
>>> True
etc.