简体   繁体   English

从另一个字典的值替换字典键

[英]Replace dictionary keys from values of another dictionary

I have three dictionaries: 我有三本字典:

packed_items = {0: [0, 3],
                2: [1], 
                1: [2]}
trucks_dict = {0: [9.5, 5.5, 5.5],
               1: [13.0, 5.5, 7.0],
               2: [16.0, 6.0, 7.0]}
items_dict = {0: [4.6, 4.3, 4.3],
              1: [4.6, 4.3, 4.3],
              2: [6.0, 5.6, 9.0],
              3: [8.75, 5.6, 6.6]}

packed_items consists of trucks as keys and values as list of items. packed_items由卡车组成,作为键,值作为项目列表。 I want to change my packed_dict such that it gives me output in this format 我想更改我的packed_dict,使其以这种格式输出

packed_dict = {[9.5, 5.5, 5.5]:[[4.6, 4.3, 4.3],[8.75, 5.6, 6.6]]
               [16.0, 6.0, 7.0]:[[4.6, 4.3, 4.3]]
               [13.0, 5.5, 7.0]:[[6.0, 5.6, 9.0]]}

Basically I want to replace my keys in packed_items with the values in trucks_dict , and values in packed_items with values in items_dict . 基本上,我想用packed_items中的值替换packed_items中的trucks_dict ,并用packed_items中的值items_dict中的items_dict

By converting your list keys to tuples, you can do that with something like: 通过将列表键转换为元组,您可以执行以下操作:

Code: 码:

result = {}
for k, v in packed_items.items():
    for i in v:
        result.setdefault(tuple(trucks_dict[k]), []).append(items_dict[i])

Test Code: 测试代码:

packed_items = {0: [0, 3],
                2: [1],
                1: [2]}
trucks_dict = {0: [9.5, 5.5, 5.5],
               1: [13.0, 5.5, 7.0],
               2: [16.0, 6.0, 7.0]}
items_dict = {0: [4.6, 4.3, 4.3],
              1: [4.6, 4.3, 4.3],
              2: [6.0, 5.6, 9.0],
              3: [8.75, 5.6, 6.6]}

result = {}
for k, v in packed_items.items():
    for i in v:
        result.setdefault(tuple(trucks_dict[k]), []).append(items_dict[i])
print(result)

Results: 结果:

{(9.5, 5.5, 5.5): [[4.6, 4.3, 4.3], [8.75, 5.6, 6.6]], 
 (16.0, 6.0, 7.0): [[4.6, 4.3, 4.3]], 
 (13.0, 5.5, 7.0): [[6.0, 5.6, 9.0]]
}

You cannot have lists as dictionary keys because they are unhashable. 您不能将列表用作字典键,因为它们不可散列。

Because you asked for string keys, you can do: 因为您要求输入字符串键,所以您可以执行以下操作:

from collections import defaultdict

packed_items = {0: [0, 3],
                2: [1], 
                1: [2]}
trucks_dict = {0: [9.5, 5.5, 5.5],
               1: [13.0, 5.5, 7.0],
               2: [16.0, 6.0, 7.0]}
items_dict = {0: [4.6, 4.3, 4.3],
              1: [4.6, 4.3, 4.3],
              2: [6.0, 5.6, 9.0],
              3: [8.75, 5.6, 6.6]}

d = defaultdict(list)

for k1, v1 in trucks_dict.items():
    for k2, v2 in items_dict.items():
        if k1 == k2 % 3:
            d[str(v1)].append(v2)

print(d)
# {'[9.5, 5.5, 5.5]': [[4.6, 4.3, 4.3], [8.75, 5.6, 6.6]], '[16.0, 6.0, 7.0]': [[4.6, 4.3, 4.3]], '[13.0, 5.5, 7.0]': [[6.0, 5.6, 9.0]]}

You can use a dict comprehension to map the lists in trucks_dict to items in items_dict . 您可以使用字典理解到在列表中映射trucks_dict在项目items_dict The lists have to be converted to tuples so that they can be hashable as keys: 列表必须转换为元组,以便它们可以作为键散列:

{tuple(trucks_dict[k]): [items_dict[i] for i in l] for k, l in packed_items.items()}

This returns: 返回:

{(9.5, 5.5, 5.5): [[4.6, 4.3, 4.3], [8.75, 5.6, 6.6]],
 (13.0, 5.5, 7.0): [[6.0, 5.6, 9.0]],
 (16.0, 6.0, 7.0): [[4.6, 4.3, 4.3]]}

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

相关问题 用另一个字典的值替换字典中的嵌套键 - Replace nested keys in a dictionary with values of another dictionary 将字典中的值作为键插入另一个字典 - Inserting values from a dictionary as keys in another dictionary 用值替换字典键 - Replace dictionary keys with values 从一个字典的键和另一个字典的值构建一个新字典 - Build a new dictionary from the keys of one dictionary and the values of another dictionary 如何用另一本字典的键替换字典的键? - How replace keys of a dictionary with keys of another dictionary? 通过比较来自另一本字典的键,从一本字典返回值 - Return values from one dictionary by comparing keys from another dictionary 如果两个字典的键匹配,则将一个字典的键替换为另一个字典的值 - Replace the keys of one dictionary with the values from another IF the keys of both dictionaries match 从另一个字典创建字典,其中键'=(上一个字典中的值)和值'=(上一个字典中的共享值) - Creating a dictionary from another dictionary where keys' = (values in previous dictionary) and value' = (shared values in previous dictionary) 如果键匹配,如何从另一个字典附加到字典值 - how to append to a dictionary values from another dictionary if keys match Python遍历字典以用另一个字典中的值替换键 - Python looping over dictionary to substitute keys with the values from another dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM