Python 我怎样编一本词典?

Python 我怎样编一本词典?,python,Python,修改为上一个任务编写的函数,使其返回字典的字典,而不是字典的列表。这本字典应该是这样的 { 1: {'Product': 'Milk', 'Brand': 'Anchor', 'Cost': '4.90'}, 2: {'Product': 'Bread', 'Brand': 'Vogel', 'Cost': '3.80'} } 这是我的密码 def run(): dic_keys = ['Product', 'Brand', 'Cost'] product

修改为上一个任务编写的函数,使其返回字典的字典,而不是字典的列表。这本字典应该是这样的

{ 
   1: {'Product': 'Milk', 'Brand': 'Anchor', 'Cost': '4.90'},   
   2: {'Product': 'Bread', 'Brand': 'Vogel', 'Cost': '3.80'} 
}
这是我的密码

def run():
    dic_keys = ['Product', 'Brand', 'Cost']
    products = {}

    is_quit = False
    while not is_quit:
    product = {}
    for key in dic_keys:
        name = input("Enter {}: ".format(key))
        if name == 'quit':
            return products

        product[key] = name
    products.append(product)  <<--- Do I need to append the product(dictionary) to the 
                                    products(dictionary outside while loop?

print(run())
现在应该是这样的:

[{'Product': 'Milk', 'Brand': 'Anchor', 'Cost': '4.90'},
 {'Product': 'Bread', 'Brand': 'Vogel', 'Cost': '3.80'}]
{ 1: {'Product': 'Milk', 'Brand': 'Anchor', 'Cost': '4.90'},
  2: {'Product': 'Bread', 'Brand': 'Vogel', 'Cost': '3.80'} }
products = {}
product = {'Product': 'Milk', 'Brand': 'Anchor', 'Cost': 4.90\
products[1] = product
def run():
   dic_keys = ['Product', 'Brand', 'Cost']
   dic_id = 1
   products = {}
   is_quit = False

   while not is_quit:
      product = {}
      for key in dic_keys:
         val = input("Enter {}: ".format(key))
         if val == "quit":
            is_quit = True
            break
         product[key] = val

      if len(product) == 3:
         products[dic_id] = product
      dic_id += 1

   return products

print(run())

字典中的字典,而不是列表中的字典。

不能在字典中附加值。要将值分配给字典,您需要像这样解析dict的键:

[{'Product': 'Milk', 'Brand': 'Anchor', 'Cost': '4.90'},
 {'Product': 'Bread', 'Brand': 'Vogel', 'Cost': '3.80'}]
{ 1: {'Product': 'Milk', 'Brand': 'Anchor', 'Cost': '4.90'},
  2: {'Product': 'Bread', 'Brand': 'Vogel', 'Cost': '3.80'} }
products = {}
product = {'Product': 'Milk', 'Brand': 'Anchor', 'Cost': 4.90\
products[1] = product
def run():
   dic_keys = ['Product', 'Brand', 'Cost']
   dic_id = 1
   products = {}
   is_quit = False

   while not is_quit:
      product = {}
      for key in dic_keys:
         val = input("Enter {}: ".format(key))
         if val == "quit":
            is_quit = True
            break
         product[key] = val

      if len(product) == 3:
         products[dic_id] = product
      dic_id += 1

   return products

print(run())
现在您可以通过products[1]访问array product中的值,因为1是值的键

我对你的问题的看法是这样的:

[{'Product': 'Milk', 'Brand': 'Anchor', 'Cost': '4.90'},
 {'Product': 'Bread', 'Brand': 'Vogel', 'Cost': '3.80'}]
{ 1: {'Product': 'Milk', 'Brand': 'Anchor', 'Cost': '4.90'},
  2: {'Product': 'Bread', 'Brand': 'Vogel', 'Cost': '3.80'} }
products = {}
product = {'Product': 'Milk', 'Brand': 'Anchor', 'Cost': 4.90\
products[1] = product
def run():
   dic_keys = ['Product', 'Brand', 'Cost']
   dic_id = 1
   products = {}
   is_quit = False

   while not is_quit:
      product = {}
      for key in dic_keys:
         val = input("Enter {}: ".format(key))
         if val == "quit":
            is_quit = True
            break
         product[key] = val

      if len(product) == 3:
         products[dic_id] = product
      dic_id += 1

   return products

print(run())

您的问题是什么?最简单的方法是使用集合。defaultdict即
dict\u of_dicts=defaultdict(dict)
您可以将dict列表转换为dict的dict试试这个
res=dict(枚举(产品,1))