简体   繁体   English

如何用对应的字典替换列表中的数字?

[英]How to replace numbers in a list of lists with its dictionary counterpart?

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). 我想尝试做的是将所有2替换为70,将所有3替换为85,将所有5替换为120(在这种情况下)。 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 ) 生成的字典基于用户输入,因此在另一种情况下,所有2可能都需要用35代替70代替。我需要一种替换数字的方式,而无需特别说明是否有2替换为70.使用此答案生成列表列表( 递归树过早终止函数

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]]

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]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM