你如何在 Python 中创建嵌套的 dict?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16333296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you create nested dict in Python?
提问by atams
I have 2 CSV files: 'Data' and 'Mapping':
我有 2 个 CSV 文件:“数据”和“映射”:
- 'Mapping' file has 4 columns:
Device_Name,GDN,Device_Type, andDevice_OS. All four columns are populated. - 'Data' file has these same columns, with
Device_Namecolumn populated and the other three columns blank. - I want my Python code to open both files and for each
Device_Namein the Data file, map itsGDN,Device_Type, andDevice_OSvalue from the Mapping file.
- '映射'文件有4列:
Device_Name,GDN,Device_Type,和Device_OS。所有四列都已填充。 - “数据”文件具有这些相同的列,其中
Device_Name填充了列,其他三列空白。 - 我希望我的Python代码来打开这两个文件并为每个
Device_Name数据文件,它的映射GDN,Device_Type以及Device_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 列(需要映射 1 列)时如何使用 dict,但是当需要映射 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.
经过一番研究,我想我需要创建一个嵌套的字典,但我不知道如何做到这一点。
采纳答案by Inbar Rose
A nested dict is a dictionary within a dictionary. A very simple thing.
嵌套字典是字典中的字典。很简单的一件事情。
>>> d = {}
>>> d['dict1'] = {}
>>> d['dict1']['innerkey'] = 'value'
>>> d
{'dict1': {'innerkey': 'value'}}
You can also use a defaultdictfrom the collectionspackage to facilitate creating nested dictionaries.
你也可以使用一个defaultdict从collections包装,以方便创建嵌套的字典。
>>> 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 likethe 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 i j k l, b.csv also has these columns. i is kind of key columns for these csvs'. j k l column is empty in a.csv but populated in b.csv. I want to map values of j k l columns using 'i` as key column from b.csv to a.csv file
可能是上面的代码混淆了这个问题。我的问题简而言之:我有 2 个文件 a.csv b.csv,a.csv 有 4 列 ijkl,b.csv 也有这些列。i 是这些 csvs 的关键列。jkl 列在 a.csv 中为空,但在 b.csv 中填充。我想将 jk l 列的值使用 'i` 作为键列从 b.csv 映射到 a.csv 文件
My suggestion would be something likethis (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 模块。
回答by Junchen
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
回答by andrew
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})})})
回答by Skysail
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,查找不存在的键可能会无意中在 dict 中创建一个新的键条目并造成大量破坏。
Here is a Python3 example with nested_dictmodule:
这是一个带有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

