Home > Blockchain >  Replacing items in a list from a dictionary
Replacing items in a list from a dictionary

Time:09-25

I'm trying to map a list and replace the values with items from a dictionary. 1.0 in the list would = 12, etc.

list_1 = list([[1.0, 2.0, 3.0, 1.0], [3.0, 1.0, 2.0, 4.0]])
dict = {1:12, 2:10, 3:5, 4:2, 5:1}

Desired output would be:

[[12, 10, 5, 12], [5, 12, 10, 2]]

CodePudding user response:

lists = [[1.0, 2.0, 3.0, 1.0], [3.0, 1.0, 2.0, 4.0]]
dic = {1:12, 2:10, 3:5, 4:2, 5:1}

result = [[dic.get(i) for i in x] for x in lists]
  • Related