0

I want to know the correct approach on how to replace numbers in a list of lists with their dictionary counterpart.

Currently my code is:

#the object list and max weight are supposed to be user input but i have hard coded it in for now to try and get other parts working first.

object_list = [(2, 70), (3, 85), (5, 120)] # the first number in the tuple is my weight and the second is my cost

max_weight = 17 #this is the number each list in the list of lists add to 

#seperatng weights and costs into a string then making a dictionary out of it.
weight_values = [int(i[0]) for i in object_list] 
cost_values = [int(i[1]) for i in object_list]
dictionary = dict(zip(weight_values, cost_values))

Lets say I have the list (all the possible combinations that add to the max weight):

[[3, 2, 2, 2, 2, 2, 2, 2], [3, 3, 3, 2, 2, 2, 2], [3, 3, 3, 3, 3, 2], [5, 2, 2, 2, 2, 2, 2], [5, 3, 3, 2, 2, 2], [5, 3, 3, 3, 3], [5, 5, 3, 2, 2], [5, 5, 5, 2]]

and the dictionary I generated is:

dictionary = {2: 70, 3: 85, 5: 120}

What I want to try and do is replace all the 2's with 70, all the 3's with 85 and all the 5's with 120 (for this case). The dictionary generated is based on user input, so in another scenario all the 2 's may need to be replaced with 35 instead of 70. I need a way of replacing the numbers without specifically saying something like if there is a 2 replace it with 70. This list of lists is generated using this answer (Recursive tree terminates function prematurely)

A0sXc950
  • 127
  • 1
  • 10
  • Welcome to SO. Unfortunately this isn't a discussion forum or tutorial. Please take the time to read [ask] and the other links on that page. Invest some time with [the Tutorial](https://docs.python.org/3/tutorial/index.html) practicing the examples. It will give you an idea of the tools Python offers to help you solve your problem. – wwii Dec 11 '17 at 20:43
  • `I need a way of replacing the numbers without specifically saying something like if there is a 2 replace it with 70.` How then would you like to tell Python, that 2 you would like to replace to `x`? – TitanFighter Dec 11 '17 at 20:46

2 Answers2

1

To replace values in the list:

l = [[3, 2, 2, 2, 2, 2, 2, 2], [3, 3, 3, 2, 2, 2, 2], [3, 3, 3, 3, 3, 2], [5, 2, 2, 2, 2, 2, 2], [5, 3, 3, 2, 2, 2], [5, 3, 3, 3, 3], [5, 5, 3, 2, 2], [5, 5, 5, 2]]
dictionary = {2: 70, 3: 85, 5: 120}
new_l = [[dictionary[b] for b in i] for i in l]

Output:

[[85, 70, 70, 70, 70, 70, 70, 70], [85, 85, 85, 70, 70, 70, 70], [85, 85, 85, 85, 85, 70], [120, 70, 70, 70, 70, 70, 70], [120, 85, 85, 70, 70, 70], [120, 85, 85, 85, 85], [120, 120, 85, 70, 70], [120, 120, 120, 70]]
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
1

To replace values:

old_list = [[3, 2, 2, 2, 2, 2, 2, 2], [3, 3, 3, 2, 2, 2, 2], [3, 3, 3, 3, 3, 2], [5, 2, 2, 2, 2, 2, 2], [5, 3, 3, 2, 2, 2], [5, 3, 3, 3, 3], [5, 5, 3, 2, 2], [5, 5, 5, 2]]
dictionary = {2: 70, 3: 85, 5: 120}
new_list = []
temp = []
for i in old_list:
    temp = []
    for b in i:
        temp.append(dictionary[b])
    new_list.append(temp)

output:

[[85, 70, 70, 70, 70, 70, 70, 70], [85, 85, 85, 70, 70, 70, 70], [85, 85, 85, 85, 85, 70], [120, 70, 70, 70, 70, 70, 70], [120, 85, 85, 70, 70, 70], [120, 85, 85, 85, 85], [120, 120, 85, 70, 70], [120, 120, 120, 70]]