【发布时间】:2021-08-24 13:02:47
【问题描述】:
我有这样的字典:
a = {"RAM": ["25GB", "256GB"], "Storage": ["25GB"]}
有没有办法制作这样的字典?
wanted_dict = [{"title":"RAM", "items":["25GB", "256GB"]}, {"title":"Storage", "items":["25GB"]}]
我尝试使用 for 循环但没有结果。 有什么建议吗?
【问题讨论】:
-
您的结果实际上是一个列表,您可以使用以下语法实现这一点:
wanted_dict = [{"title": key, "items": value} for key, value in a.items()]。其中a是您的字典名称。 -
显示你的循环及其结果。
-
minimal reproducible example for for 循环丢失,“没有结果”是什么意思?
标签: python python-3.x dictionary