简体   繁体   English

如何在 Python 中创建嵌套字典?

[英]How do you create nested dict in Python?

I have 2 CSV files: 'Data' and 'Mapping':我有 2 个 CSV 文件:“数据”和“映射”:

  • 'Mapping' file has 4 columns: Device_Name , GDN , Device_Type , and Device_OS . “映射”文件有 4 列: Device_NameGDNDevice_TypeDevice_OS All four columns are populated.所有四列均已填充。
  • 'Data' file has these same columns, with Device_Name column populated and the other three columns blank. “数据”文件具有这些相同的列,填充了Device_Name列,其他三列为空白。
  • I want my Python code to open both files and for each Device_Name in the Data file, map its GDN , Device_Type , and Device_OS value from the Mapping file.我希望我的 Python 代码打开这两个文件,并为数据文件中的每个Device_Name从映射文件映射其GDNDevice_TypeDevice_OS值。

I know how to use dict when only 2 columns are present (1 is needed to be mapped) but I don't know how to accomplish this when 3 columns need to be mapped.我知道当只有 2 列存在时如何使用 dict(需要映射 1 列)但我不知道如何在需要映射 3 列时完成此操作。

Following is the code using which I tried to accomplish mapping of Device_Type :以下是我用来完成Device_Type映射的代码:

x = dict([])
with open("Pricing Mapping_2013-04-22.csv", "rb") as in_file1:
    file_map = csv.reader(in_file1, delimiter=',')
    for row in file_map:
       typemap = [row[0],row[2]]
       x.append(typemap)

with open("Pricing_Updated_Cleaned.csv", "rb") as in_file2, open("Data Scraper_GDN.csv", "wb") as out_file:
    writer = csv.writer(out_file, delimiter=',')
    for row in csv.reader(in_file2, delimiter=','):
         try:
              row[27] = x[row[11]]
         except KeyError:
              row[27] = ""
         writer.writerow(row)

It returns Attribute Error .它返回Attribute Error

After some researching, I think I need to create a nested dict, but I don't have any idea how to do this.经过一些研究,我想我需要创建一个嵌套的字典,但我不知道该怎么做。

A nested dict is a dictionary within a dictionary.嵌套字典是字典中的字典。 A very simple thing.很简单的一件事情。

>>> d = {}
>>> d['dict1'] = {}
>>> d['dict1']['innerkey'] = 'value'
>>> d['dict1']['innerkey2'] = 'value2'
>>> d
{'dict1': {'innerkey': 'value', 'innerkey2': 'value2'}}

You can also use a defaultdict from the collections package to facilitate creating nested dictionaries.您还可以使用collections包中的defaultdict来方便创建嵌套字典。

>>> import collections
>>> d = collections.defaultdict(dict)
>>> d['dict1']['innerkey'] = 'value'
>>> d  # currently a defaultdict type
defaultdict(<type 'dict'>, {'dict1': {'innerkey': 'value'}})
>>> dict(d)  # but is exactly like a normal dictionary.
{'dict1': {'innerkey': 'value'}}

You can populate that however you want.您可以随意填充。

I would recommend in your code something like the following:我建议在你的代码下面这样

d = {}  # can use defaultdict(dict) instead

for row in file_map:
    # derive row key from something 
    # when using defaultdict, we can skip the next step creating a dictionary on row_key
    d[row_key] = {} 
    for idx, col in enumerate(row):
        d[row_key][idx] = col

According to your comment :根据您的评论

may be above code is confusing the question.可能是上面的代码混淆了这个问题。 My problem in nutshell: I have 2 files a.csv b.csv, a.csv has 4 columns ijkl, b.csv also has these columns.我的问题简而言之:我有 2 个文件 a.csv b.csv,a.csv 有 4 列 ijkl,b.csv 也有这些列。 i is kind of key columns for these csvs'. i 是这些 csvs 的关键列。 jkl column is empty in a.csv but populated in b.csv. jkl 列在 a.csv 中为空,但在 b.csv 中填充。 I want to map values of jkl columns using 'i` as key column from b.csv to a.csv file我想将 jkl 列的值使用“i”作为键列从 b.csv 映射到 a.csv 文件

My suggestion would be something like this (without using defaultdict):我的建议是这样的(不使用defaultdict):

a_file = "path/to/a.csv"
b_file = "path/to/b.csv"

# read from file a.csv
with open(a_file) as f:
    # skip headers
    f.next()
    # get first colum as keys
    keys = (line.split(',')[0] for line in f) 

# create empty dictionary:
d = {}

# read from file b.csv
with open(b_file) as f:
    # gather headers except first key header
    headers = f.next().split(',')[1:]
    # iterate lines
    for line in f:
        # gather the colums
        cols = line.strip().split(',')
        # check to make sure this key should be mapped.
        if cols[0] not in keys:
            continue
        # add key to dict
        d[cols[0]] = dict(
            # inner keys are the header names, values are columns
            (headers[idx], v) for idx, v in enumerate(cols[1:]))

Please note though, that for parsing csv files there is a csv module .但请注意,解析 csv 文件有一个csv 模块

UPDATE : For an arbitrary length of a nested dictionary, go to this answer .更新:对于任意长度的嵌套字典,请转到此答案

Use the defaultdict function from the collections.使用集合中的 defaultdict 函数。

High performance: "if key not in dict" is very expensive when the data set is large.高性能:当数据集很大时,“if key not in dict”非常昂贵。

Low maintenance: make the code more readable and can be easily extended.低维护:使代码更具可读性,易于扩展。

from collections import defaultdict

target_dict = defaultdict(dict)
target_dict[key1][key2] = val

For arbitrary levels of nestedness:对于任意级别的嵌套:

In [2]: def nested_dict():
   ...:     return collections.defaultdict(nested_dict)
   ...:

In [3]: a = nested_dict()

In [4]: a
Out[4]: defaultdict(<function __main__.nested_dict>, {})

In [5]: a['a']['b']['c'] = 1

In [6]: a
Out[6]:
defaultdict(<function __main__.nested_dict>,
            {'a': defaultdict(<function __main__.nested_dict>,
                         {'b': defaultdict(<function __main__.nested_dict>,
                                      {'c': 1})})})

It is important to remember when using defaultdict and similar nested dict modules such as nested_dict , that looking up a nonexistent key may inadvertently create a new key entry in the dict and cause a lot of havoc.在使用 defaultdict 和类似的嵌套 dict 模块(例如nested_dict ,请nested_dict ,查找不存在的键可能会无意中在 dict 中创建一个新的键条目并造成大量破坏。

Here is a Python3 example with nested_dict module:这是一个带有nested_dict模块的Python3 示例:

import nested_dict as nd
nest = nd.nested_dict()
nest['outer1']['inner1'] = 'v11'
nest['outer1']['inner2'] = 'v12'
print('original nested dict: \n', nest)
try:
    nest['outer1']['wrong_key1']
except KeyError as e:
    print('exception missing key', e)
print('nested dict after lookup with missing key.  no exception raised:\n', nest)

# Instead, convert back to normal dict...
nest_d = nest.to_dict(nest)
try:
    print('converted to normal dict. Trying to lookup Wrong_key2')
    nest_d['outer1']['wrong_key2']
except KeyError as e:
    print('exception missing key', e)
else:
    print(' no exception raised:\n')

# ...or use dict.keys to check if key in nested dict
print('checking with dict.keys')
print(list(nest['outer1'].keys()))
if 'wrong_key3' in list(nest.keys()):

    print('found wrong_key3')
else:
    print(' did not find wrong_key3')

Output is:输出是:

original nested dict:   {"outer1": {"inner2": "v12", "inner1": "v11"}}

nested dict after lookup with missing key.  no exception raised:  
{"outer1": {"wrong_key1": {}, "inner2": "v12", "inner1": "v11"}} 

converted to normal dict. 
Trying to lookup Wrong_key2 

exception missing key 'wrong_key2' 

checking with dict.keys 

['wrong_key1', 'inner2', 'inner1']  
did not find wrong_key3

If you want to create a nested dictionary given a list (arbitrary length) for a path and perform a function on an item that may exist at the end of the path, this handy little recursive function is quite helpful:如果你想创建一个给定路径列表(任意长度)的嵌套字典,并在路径末尾可能存在的项目上执行一个函数,这个方便的小递归函数非常有用:

def ensure_path(data, path, default=None, default_func=lambda x: x):
    """
    Function:

    - Ensures a path exists within a nested dictionary

    Requires:

    - `data`:
        - Type: dict
        - What: A dictionary to check if the path exists
    - `path`:
        - Type: list of strs
        - What: The path to check

    Optional:

    - `default`:
        - Type: any
        - What: The default item to add to a path that does not yet exist
        - Default: None

    - `default_func`:
        - Type: function
        - What: A single input function that takes in the current path item (or default) and adjusts it
        - Default: `lambda x: x` # Returns the value in the dict or the default value if none was present
    """
    if len(path)>1:
        if path[0] not in data:
            data[path[0]]={}
        data[path[0]]=ensure_path(data=data[path[0]], path=path[1:], default=default, default_func=default_func)
    else:
        if path[0] not in data:
            data[path[0]]=default
        data[path[0]]=default_func(data[path[0]])
    return data

Example:例子:

data={'a':{'b':1}}
ensure_path(data=data, path=['a','c'], default=[1])
print(data) #=> {'a':{'b':1, 'c':[1]}}
ensure_path(data=data, path=['a','c'], default=[1], default_func=lambda x:x+[2])
print(data) #=> {'a': {'b': 1, 'c': [1, 2]}}

This thing is empty nested list from which ne will append data to empty dict这东西是空的嵌套列表,ne 将从中将数据附加到空的 dict

ls = [['a','a1','a2','a3'],['b','b1','b2','b3'],['c','c1','c2','c3'], 
['d','d1','d2','d3']]

this means to create four empty dict inside data_dict这意味着在 data_dict 中创建四个空字典

data_dict = {f'dict{i}':{} for i in range(4)}
for i in range(4):
    upd_dict = {'val' : ls[i][0], 'val1' : ls[i][1],'val2' : ls[i][2],'val3' : ls[i][3]}

    data_dict[f'dict{i}'].update(upd_dict)

print(data_dict)

The output输出

{'dict0': {'val': 'a', 'val1': 'a1', 'val2': 'a2', 'val3': 'a3'}, 'dict1': {'val': 'b', 'val1': 'b1', 'val2': 'b2', 'val3': 'b3'},'dict2': {'val': 'c', 'val1': 'c1', 'val2': 'c2', 'val3': 'c3'}, 'dict3': {'val': 'd', 'val1': 'd1', 'val2': 'd2', 'val3': 'd3'}} {'dict0':{'val':'a','val1':'a1','val2':'a2','val3':'a3'},'dict1':{'val':'b ', 'val1': 'b1', 'val2': 'b2', 'val3': 'b3'},'dict2': {'val': 'c', 'val1': 'c1', 'val2 ':'c2','val3':'c3'},'dict3':{'val':'d','val1':'d1','val2':'d2','val3':'d3 '}}

#in jupyter
import sys
!conda install -c conda-forge --yes --prefix {sys.prefix} nested_dict 
import nested_dict as nd
d = nd.nested_dict()

'd' can be used now to store the nested key value pairs.现在可以使用“d”来存储嵌套的键值对。

travel_log = {
    "France" : {"cities_visited" : ["paris", "lille", "dijon"], "total_visits" : 10},
    "india" : {"cities_visited" : ["Mumbai", "delhi", "surat",], "total_visits" : 12}
}
pip install addict
from addict import Dict

mapping = Dict()
mapping.a.b.c.d.e = 2
print(mapping)  # {'a': {'b': {'c': {'d': {'e': 2}}}}}

References:参考:

  1. easydict GitHub易字典 GitHub
  2. addict GitHub GitHub 上瘾者

You can initialize an empty NestedDict and then assign values to new keys.您可以初始化一个空的NestedDict ,然后将值分配给新键。

from ndicts.ndicts import NestedDict

nd = NestedDict()
nd["level1", "level2", "level3"] = 0
>>> nd
NestedDict({'level1': {'level2': {'level3': 0}}})

ndicts is on Pypi ndicts在 Pypi 上

pip install ndicts

You can create simple class inherited from dict and implement only __missing__ method:您可以创建从 dict 继承的简单类并仅实现__missing__方法:

class NestedDict(dict):
    def __missing__(self, x):
        self[x] = NestedDict()
        return self[x]
d = NestedDict()
d[1][2] = 3
print(d)
# {1: {2: 3}}

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

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