簡體   English   中英

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

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

我有 2 個 CSV 文件:“數據”和“映射”:

  • “映射”文件有 4 列: Device_NameGDNDevice_TypeDevice_OS 所有四列均已填充。
  • “數據”文件具有這些相同的列,填充了Device_Name列,其他三列為空白。
  • 我希望我的 Python 代碼打開這兩個文件,並為數據文件中的每個Device_Name從映射文件映射其GDNDevice_TypeDevice_OS值。

我知道當只有 2 列存在時如何使用 dict(需要映射 1 列)但我不知道如何在需要映射 3 列時完成此操作。

以下是我用來完成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)

它返回Attribute Error

經過一些研究,我想我需要創建一個嵌套的字典,但我不知道該怎么做。

嵌套字典是字典中的字典。 很簡單的一件事情。

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

您還可以使用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'}}

您可以隨意填充。

我建議在你的代碼下面這樣

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

根據您的評論

可能是上面的代碼混淆了這個問題。 我的問題簡而言之:我有 2 個文件 a.csv b.csv,a.csv 有 4 列 ijkl,b.csv 也有這些列。 i 是這些 csvs 的關鍵列。 jkl 列在 a.csv 中為空,但在 b.csv 中填充。 我想將 jkl 列的值使用“i”作為鍵列從 b.csv 映射到 a.csv 文件

我的建議是這樣的(不使用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:]))

但請注意,解析 csv 文件有一個csv 模塊

更新:對於任意長度的嵌套字典,請轉到此答案

使用集合中的 defaultdict 函數。

高性能:當數據集很大時,“if key not in dict”非常昂貴。

低維護:使代碼更具可讀性,易於擴展。

from collections import defaultdict

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

對於任意級別的嵌套:

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})})})

在使用 defaultdict 和類似的嵌套 dict 模塊(例如nested_dict ,請nested_dict ,查找不存在的鍵可能會無意中在 dict 中創建一個新的鍵條目並造成大量破壞。

這是一個帶有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')

輸出是:

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

如果你想創建一個給定路徑列表(任意長度)的嵌套字典,並在路徑末尾可能存在的項目上執行一個函數,這個方便的小遞歸函數非常有用:

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

例子:

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]}}

這東西是空的嵌套列表,ne 將從中將數據附加到空的 dict

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

這意味着在 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)

輸出

{'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”來存儲嵌套的鍵值對。

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}}}}}

參考:

  1. 易字典 GitHub
  2. GitHub 上癮者

您可以初始化一個空的NestedDict ,然后將值分配給新鍵。

from ndicts.ndicts import NestedDict

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

ndicts在 Pypi 上

pip install ndicts

您可以創建從 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