I have a list.
mapper = {"a": 9, "b": 7}
A = ["a", "b"]
And I want to get:
result = [9, 7]
I know there are multiple ways to achieve that, like:
result = [mapper[char] for char in A]
result = list(map(lambda x: mapper[x], A))
For the second way, could we use operator
module instead of using lambda?
I found a method called operator.getitem()
,and I try to use
result = list(map(operator.getitem(mapper), A))
But this will raise an exception.
I know list(map(lambda x: operator.getitem(mapper, x), A))
will work, but I just want to avoid using lambda
.
I have found this question, but I didn't find a solution.