简体   繁体   English

将值附加到 python 中嵌套字典内的列表

[英]Appending values to a list inside nested dictionary in python

I am having trouble appending values to a list housed within nested dictionaries.我无法将值附加到嵌套字典中的列表。 I am trying to track crops planted on a set of fields over several years.我正在尝试跟踪几年来在一组田地上种植的农作物。 My goal is to be able to search for a list of crops in a manner like crops[year][field_name] .我的目标是能够以类似crops[year][field_name]的方式搜索作物列表。

Here is the empty data structure:这是空的数据结构:

{2015: {'CP 4B1': [],
  'CP 4D1': [],
  'CP 1': [],
  'CP 5D': [],
  'CP 6C': [],
  'CP 3': [],
  'CP 4B': [],
  'CP 4D': [],
  'CP 4A': [],
  'CP 4C1': [],
  'CP 5C': [],
  'CP 6A': [],
  'CP 4A1': [],
  'EF 4A': [],
  'EF 5B': [],
  'EF 5A': [],
  'EF 1D': [],
  'EF 1E': [],
  'EF 1C': [],
  'ES 6A': [],
  'ES 6B': [],
  'EF 1A': [],
  'EF 1B': [],
  'EF 4C': [],
  'EF 4B': [],
  'FF 1': [],
  'FF 2': [],
  'FF 3': [],
  'HM 11': [],
  'HM 1': [],
  'HM 16B': [],
  'HM 2A': [],
  'HM 9C': [],
  'HM 9B': [],
  'HM 10': [],
  'HM 12': [],
  'HM 5': [],
  'HM 8': [],
  'HM 13': [],
  'HM 2B': [],
  'HM 6': [],
  'HF 13': [],
  'HF 12': [],
  'HF 2A': [],
  'HF 15B': [],
  'HF 15A': [],
  'HF 9': [],
  'MC 8': [],
  'MC 10': [],
  'MC 5': [],
  'MC 1': [],
  'MC 2': [],
  'MC 13': [],
  'MC 4': [],
  'MC 3': [],
  'MC 6': [],
  'MC 12': [],
  'MC 9': [],
  'MC 7': [],
  'MC 11A': [],
  'MC 11': [],
  'MC 14': [],
  'MC 14A': [],
  'MC 13A': [],
  'MC 2A': [],
  'RF 2A': [],
  'MS 8': [],
  'MS 16': [],
  'MS 21': [],
  'MS 17': [],
  'MS 11': [],
  'BSF 1B': [],
  'BSF 4A': [],
  'BSF 3': [],
  'BSF 1A': [],
  'BSF Orchard': [],
  'TF 4': [],
  'TF 3B': [],
  'TF 3A': [],
  'TF 5': [],
  'TF 11': [],
  'TF 8': [],
  'TF 1': [],
  'TF 2': [],
  'WA 3': [],
  'WA 7': [],
  'WA 5B': [],
  'WA 1': [],
  'WA 5A': []},
 2016: {'CP 4B1': [],
  'CP 4D1': [],
  'CP 1': [],
  'CP 5D': [],
  'CP 6C': [],
  'CP 3': [],
  'CP 4B': [],
  'CP 4D': [],
  'CP 4A': [],
  'CP 4C1': [],
  'CP 5C': [],
  'CP 6A': [],
  'CP 4A1': [],
  'EF 4A': [],
  'EF 5B': [],
  'EF 5A': [],
  'EF 1D': [],
  'EF 1E': [],
  'EF 1C': [],
  'ES 6A': [],
  'ES 6B': [],
  'EF 1A': [],
  'EF 1B': [],
  'EF 4C': [],
  'EF 4B': [],
  'FF 1': [],
  'FF 2': [],
  'FF 3': [],
  'HM 11': [],
  'HM 1': [],
  'HM 16B': [],
  'HM 2A': [],
  'HM 9C': [],
  'HM 9B': [],
  'HM 10': [],
  'HM 12': [],
  'HM 5': [],
  'HM 8': [],
  'HM 13': [],
  'HM 2B': [],
  'HM 6': [],
  'HF 13': [],
  'HF 12': [],
  'HF 2A': [],
  'HF 15B': [],
  'HF 15A': [],
  'HF 9': [],
  'MC 8': [],
  'MC 10': [],
  'MC 5': [],
  'MC 1': [],
  'MC 2': [],
  'MC 13': [],
  'MC 4': [],
  'MC 3': [],
  'MC 6': [],
  'MC 12': [],
  'MC 9': [],
  'MC 7': [],
  'MC 11A': [],
  'MC 11': [],
  'MC 14': [],
  'MC 14A': [],
  'MC 13A': [],
  'MC 2A': [],
  'RF 2A': [],
  'MS 8': [],
  'MS 16': [],
  'MS 21': [],
  'MS 17': [],
  'MS 11': [],
  'BSF 1B': [],
  'BSF 4A': [],
  'BSF 3': [],
  'BSF 1A': [],
  'BSF Orchard': [],
  'TF 4': [],
  'TF 3B': [],
  'TF 3A': [],
  'TF 5': [],
  'TF 11': [],
  'TF 8': [],
  'TF 1': [],
  'TF 2': [],
  'WA 3': [],
  'WA 7': [],
  'WA 5B': [],
  'WA 1': [],
  'WA 5A': []},

etc.等等

I see that I can access the list by following the desired path: crops[year]["field_name"] , but when I try to append to that list using crops[year]["field_name"].append('Corn') , the program appends 'Corn' to all lists with that field name regardless of year.我看到我可以按照所需的路径访问该列表: crop crops[year]["field_name"] ,但是当我尝试使用 crop crops[year]["field_name"].append('Corn')将 append 到该列表时,无论年份如何,程序都会将“Corn”附加到具有该字段名称的所有列表中。 So crops[2015]["Field 1"] will have Corn, but so will crops[2016]["Field 1"] and crops[2017]["Field 1"] even when I only intend to append it to the list at crops[2015]["Field 1"] using crops[2015]["Field 1"].append('Corn')所以crops[2015]["Field 1"]会有玉米,但是 crop crops[2016]["Field 1"]和 crop crops[2017]["Field 1"]即使我只打算 append 到列表中在crops[2015]["Field 1"] 使用crops[2015]["Field 1"].append('Corn')

I may just be confused about how to navigate the data structure, but any help would be greatly appreciated.我可能只是对如何导航数据结构感到困惑,但任何帮助将不胜感激。

It works just fine as you intended它可以按您的预期正常工作

crop_dict = {
  2015: {
  'CP 4B1': [],
  'CP 4D1': []
  },
  2016: {
  'CP 4B1': [],
  'CP 4D1': [],
  }}
crop_dict[2015]["CP 4B1"].append('Corn')
print(crop_dict[2015]["CP 4B1"])
print(crop_dict[2016]["CP 4B1"])

OUTPUT: OUTPUT:

['Corn']
[]

You should follow this syntax to append/add new values to your dictionary, there are a few ways to go about it, try something like these two options ( this is an example , you will have to implement it for your own problem, hope it helps):您应该按照此语法向您的字典追加/添加新值,有几种方法可以 go 关于它,尝试类似这两个选项(这是一个示例,您必须为自己的问题实现它,希望它帮助):

food_dict = {
   'Sauce': {
      'Tomato': [],
      'Mayo': []
   }
}

food_dict['Sauce']['Tomato'] = [5]

## or

for k in food_dict.keys():
  if k == "Sauce":
     food_dict[k]['Mayo'] = [100]
  

print(food_dict)

Output: Output:

{'Sauce': {'Tomato': [5], 'Mayo': [100]}}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM