从不同列表创建字典 Python 3

huangapple go评论81阅读模式
英文:

dictionary from different lists python 3

问题

I have the following lists:

list1 = ["Hulk", "Flash", "Tesla"]
list2 = ["green", "23", "thunder"]
list3 = ["Marvel", "DC_comics", "0.0"]

and I would like to create a dictionary like this one:

{
    "eo:dict": [
        {"name": "Hulk", "color": "green", "company": "Marvel"},
        {"name": "Flash", "color": "red", "company": "DC_comics"},
        {"name": "Tesla", "color": "thunder", "company": 0.0},
    ]
}

that has to be appended in a specific position within a json file I'm creating.

I tried in this way:

keys = ["name", "company", "colours"]
eo_dict = dict(zip(keys, name, company, color))

but I got the error "ValueError: dictionary update sequence element #0 has length 3; 2 is required."

Could anybody give me some hints?

英文:

I have the following lists:

list1 = ["Hulk", "Flash", "Tesla"]
list2 = ["green", "23", "thunder"]
list3 = ["Marvel", "DC_comics", "0.0"]

and I would like to create a dictionary like this one:

{
    "eo:dict": [
        {"name": "Hulk", "color": green, "company": Marvel},
        {"name": "Flash", "color": red, "company": DC_comics},
        {"name": "Tesla", "color": thunder, "company": 0.0},
    ]
}

that has to be appended in a specific position within a json file I'm creating.

I tried in this way:

keys = ["name", "company", "colours"]
eo_dict = dict(zip(keys, name,company, color))

but I got the error "_ValueError: dictionary update sequence element #0 has length 3; 2 is required_"

Could anybody give me some hints?

答案1

得分: 2

以下是翻译的部分:

list1 = ['Hulk', 'Flash', 'Tesla']
list2 = ['green', '23', 'thunder']
list3 = ['Marvel', 'DC_comics', '0.0']
keys = ['name', 'color', 'company']

out = list(dict(zip(keys, values)) for values in zip(list1, list2, list3))

输出:

>>> out
[{'name': 'Hulk', 'color': 'green', 'company': 'Marvel'},
 {'name': 'Flash', 'color': '23', 'company': 'DC_comics'},
 {'name': 'Tesla', 'color': 'thunder', 'company': '0.0'}]

导出为JSON:

import json

jd = json.dumps([{'eo:dict': out}], indent=4)
print(jd)

# 输出
[
    {
        "eo:dict": [
            {
                "name": "Hulk",
                "color": "green",
                "company": "Marvel"
            },
            {
                "name": "Flash",
                "color": "23",
                "company": "DC_comics"
            },
            {
                "name": "Tesla",
                "color": "thunder",
                "company": "0.0"
            }
        ]
    }
]

希望这些对您有所帮助!

英文:

You can use this code to get the expected output:

list1 = ['Hulk', 'Flash', 'Tesla']
list2 = ['green', '23', 'thunder']
list3 = ['Marvel', 'DC_comics', '0.0']
keys = ['name', 'color', 'company']

out = list(dict(zip(keys, values)) for values in zip(list1, list2, list3))

Output:

>>> out
[{'name': 'Hulk', 'color': 'green', 'company': 'Marvel'},
 {'name': 'Flash', 'color': '23', 'company': 'DC_comics'},
 {'name': 'Tesla', 'color': 'thunder', 'company': '0.0'}]

To export as JSON:

import json

jd = json.dumps([{'eo:dict': out}], indent=4)
print(jd)

# Output
[
    {
        "eo:dict": [
            {
                "name": "Hulk",
                "color": "green",
                "company": "Marvel"
            },
            {
                "name": "Flash",
                "color": "23",
                "company": "DC_comics"
            },
            {
                "name": "Tesla",
                "color": "thunder",
                "company": "0.0"
            }
        ]
    }
]

答案2

得分: 1

You should try this one,

keys = ["name", "age", "location"]
values = ["John", 28, "New York"]

my_dict = {k:v for k, v in zip(keys, values)}

print(my_dict)

The output is like

{'name': 'John', 'age': 28, 'location': 'New York'}

英文:

You should try this one,

keys = ["name", "age", "location"]
values = ["John", 28, "New York"]

my_dict = {k:v for k, v in zip(keys, values)}

print(my_dict)

The output is like

{'name': 'John', 'age': 28, 'location': 'New York'}

huangapple
  • 本文由 发表于 2023年4月13日 14:59:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定