从不同列表创建字典 Python 3

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

dictionary from different lists python 3

问题

I have the following lists:

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

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

  1. {
  2. "eo:dict": [
  3. {"name": "Hulk", "color": "green", "company": "Marvel"},
  4. {"name": "Flash", "color": "red", "company": "DC_comics"},
  5. {"name": "Tesla", "color": "thunder", "company": 0.0},
  6. ]
  7. }

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

I tried in this way:

  1. keys = ["name", "company", "colours"]
  2. 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:

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

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

  1. {
  2. "eo:dict": [
  3. {"name": "Hulk", "color": green, "company": Marvel},
  4. {"name": "Flash", "color": red, "company": DC_comics},
  5. {"name": "Tesla", "color": thunder, "company": 0.0},
  6. ]
  7. }

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

I tried in this way:

  1. keys = ["name", "company", "colours"]
  2. 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

以下是翻译的部分:

  1. list1 = ['Hulk', 'Flash', 'Tesla']
  2. list2 = ['green', '23', 'thunder']
  3. list3 = ['Marvel', 'DC_comics', '0.0']
  4. keys = ['name', 'color', 'company']
  5. out = list(dict(zip(keys, values)) for values in zip(list1, list2, list3))

输出:

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

导出为JSON:

  1. import json
  2. jd = json.dumps([{'eo:dict': out}], indent=4)
  3. print(jd)
  4. # 输出
  5. [
  6. {
  7. "eo:dict": [
  8. {
  9. "name": "Hulk",
  10. "color": "green",
  11. "company": "Marvel"
  12. },
  13. {
  14. "name": "Flash",
  15. "color": "23",
  16. "company": "DC_comics"
  17. },
  18. {
  19. "name": "Tesla",
  20. "color": "thunder",
  21. "company": "0.0"
  22. }
  23. ]
  24. }
  25. ]

希望这些对您有所帮助!

英文:

You can use this code to get the expected output:

  1. list1 = ['Hulk', 'Flash', 'Tesla']
  2. list2 = ['green', '23', 'thunder']
  3. list3 = ['Marvel', 'DC_comics', '0.0']
  4. keys = ['name', 'color', 'company']
  5. out = list(dict(zip(keys, values)) for values in zip(list1, list2, list3))

Output:

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

To export as JSON:

  1. import json
  2. jd = json.dumps([{'eo:dict': out}], indent=4)
  3. print(jd)
  4. # Output
  5. [
  6. {
  7. "eo:dict": [
  8. {
  9. "name": "Hulk",
  10. "color": "green",
  11. "company": "Marvel"
  12. },
  13. {
  14. "name": "Flash",
  15. "color": "23",
  16. "company": "DC_comics"
  17. },
  18. {
  19. "name": "Tesla",
  20. "color": "thunder",
  21. "company": "0.0"
  22. }
  23. ]
  24. }
  25. ]

答案2

得分: 1

You should try this one,

  1. keys = ["name", "age", "location"]
  2. values = ["John", 28, "New York"]
  3. my_dict = {k:v for k, v in zip(keys, values)}
  4. print(my_dict)

The output is like

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

英文:

You should try this one,

  1. keys = ["name", "age", "location"]
  2. values = ["John", 28, "New York"]
  3. my_dict = {k:v for k, v in zip(keys, values)}
  4. 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:

确定