I want to pass a full array into a dictionary as keys and get the values out:
dic = {1:'a', 2:'b', 3:'c'}
lista = [1,2,3]
dic.get(1)
'a'
dic.get(list)
error
thanks
CodePudding user response:
You could use operator.itemgetter here.
from operator import itemgetter
dic = {1:'a', 2:'b', 3:'c'}
lst = [1, 2, 3]
itemgetter(*lst)(dic)
# ('a', 'b', 'c')
CodePudding user response:
Use -
dic = {1:'a', 2:'b', 3:'c'}
list_ = [1,2,3]
a = [dic.get(k) for k in list_]
@Ch3steR
453 ns ± 192 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
-->thisone
888 ns ± 430 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
CodePudding user response:
in pandas its called the map function:
lista = lista.map(dic)
cheers
CodePudding user response:
We can do map
[*map(dic.get,lista)]
Out[270]: ['a', 'b', 'c']
