I don't know how to decrement from an object in a list.
For example:
a = 1
b = 2
c = 3
lst = [a, b, c]
choice = a
choice = choice - 2
When I do that it doesn't deduct from a
CodePudding user response:
You use indexing to access list elements:
a = 1
b = 2
c = 3
list = [a, b, c]
choice = 0 # the index of `a`
lst[choice] -= 2
This won't change the value of a
because integers are immutable in python. Only the reference stored in the list will change.
You can find the index by value using list.index
:
choice = lst.index(a)
If you want to search by name instead of by value, use a dictionary instead of a list:
d = {
'a' = 1,
'b' = 2,
'c' = 3
}
d['a'] -= 2