我需要在Python中创建一个二维字典。例如:new_dic[1][2] = 5
当我创建new_dic = {}
并尝试插入值时,我会遇到KeyError
错误:
new_dic[1][2] = 5
KeyError: 1
如何做到这一点?
我需要在Python中创建一个二维字典。例如:new_dic[1][2] = 5
当我创建new_dic = {}
并尝试插入值时,我会遇到KeyError
错误:
new_dic[1][2] = 5
KeyError: 1
如何做到这一点?
多维字典就是以字典为值的字典,形成了嵌套结构:
new_dic = {}
new_dic[1] = {}
new_dic[1][2] = 5
为了避免在new_dic [1]
下的其他键中意外清除该嵌套对象,您需要每次检测是否已经创建了new_dic [1]
。
您可以使用各种技术简化创建嵌套字典;例如使用dict.setdefault()
:
new_dic.setdefault(1, {})[2] = 5
dict.setdefault()
只有在键值不存在时才将一个键值对设置为默认值,使您免于每次测试此操作的麻烦。
更简单的方法是使用collections.defaultdict()
类型自动创建嵌套字典:
from collections import defaultdict
new_dic = defaultdict(dict)
new_dic[1][2] = 5
defaultdict
只是标准 dict
类型的一个子类;每当您尝试访问映射中还不存在的键时,就会调用工厂函数来创建新值。这里使用的是dict()
可调用对象,在调用时生成一个空字典。
演示:
>>> new_dic_plain = {}
>>> new_dic_plain[1] = {}
>>> new_dic_plain[1][2] = 5
>>> new_dic_plain
{1: {2: 5}}
>>> new_dic_setdefault = {}
>>> new_dic_setdefault.setdefault(1, {})[2] = 5
>>> new_dic_setdefault
{1: {2: 5}}
>>> from collections import defaultdict
>>> new_dic_defaultdict = defaultdict(dict)
>>> new_dic_defaultdict[1][2] = 5
>>> new_dic_defaultdict
defaultdict(<type 'dict'>, {1: {2: 5}})
看一下这个:
def nested_dict(n, type):
if n == 1:
return defaultdict(type)
else:
return defaultdict(lambda: nested_dict(n-1, type))
然后:
new_dict = nested_dict(2, float)
现在您可以:
new_dict['key1']['key2'] += 5
您可以创建任意数量的维度,选择您想要的目标类型:new_dict = nested_dict(3, list)
new_dict['a']['b']['c'].append(5)
结果将是:
new_dict['a']['b']['c'] = [5]
一种简单的方法是将元组作为常规字典的键。因此,您的示例变为:
new_dic[(1, 2)] = 5
缺点是所有用法都必须遵循这种略显笨拙的约定,但如果可以接受,那么这就是您所需要的。
new_dic[("orange", "apple", "coconut")] = {"": ""}
- ktangnew_dic[1,2] = 5
同样可以使用元组(1,2)
作为键。 - markusk简单来说,你可以使用defaultdict
from collections import defaultdict
new_dic = defaultdict(dict)
new_dic[1][2]=5
>>>new_dic
defaultdict(<type 'dict'>, {1: {2: 5}})
您可以尝试这个,如果它是字符串,那么它甚至更容易。
new_dic = {}
a = 1
new_dic[a] = {}
b = 2
new_dic[a][b] = {}
c = 5
new_dic[a][b]={c}
new_dic[a][b]
>>>'5'
针对字符串类型
new_dic = {}
a = "cat"
new_dic[a] = {}
b = "dog"
new_dic[a][b] = {}
c = 5
new_dic[a][b] = {c}
类型
new_dic["cat"]["dog"]
>>>'5'
dict
还是list
?dict
,你想要第二层是另一个dict
还是一个list
?dict
,需要事先声明键。dicts
中嵌套dicts
,需要像这样做:new_dic = {}
try:
new_dic[1][2] = 5
except KeyError:
new_dic[1] = {2:5}
这里有一个包含另一个字典作为键1值的字典:
>>> new_dic = {}
>>> new_dic[1] = {2:5}
>>> new_dic
{1: {2: 5}}
new_dic={}
new_dic[1][2]=5
问题在于new_dic[1]
不存在,所以你无法向它添加字典(或其他任何东西)。
class myCoordinates:
def __init__(self, args)
self.x = args[0]
self.y = args[1]
然后当我尝试创建字典时:
table = mult_dim_dict(2, myCoordinates, (30, 30))
函数'mult_dim_dict'的定义如下:
def mult_dim_dict(dim, dict_type, params):
if dim == 1:
if params is not None:
return defaultdict(lambda: dict_type(params))
else:
return defaultdict(dict_type)
else:
return defaultdict(lambda: mult_dim_dict(dim - 1, dict_type, params))
None
:class myCoors:
def __init__(self, tuple=(0, 0)):
self.x, self.y = tuple
def printCoors(self):
print("x = %d, y = %d" %(self.x, self.y))
def mult_dim_dict(dim, dict_type, params):
if dim == 1:
if params is not None:
return defaultdict(lambda: dict_type(params))
else:
return defaultdict(dict_type)
else:
return defaultdict(lambda: mult_dim_dict(dim - 1, dict_type, params))
dict = mult_dim_dict(2, myCoors, None)
dict['3']['2'].x = 3
dict['3']['2'].y = 2
dict['3']['2'].printCoors() # x = 3, y = 2 will be printed
dict = mult_dim_dict(2, myCoors, (30, 20))
dict['3']['2'].printCoors() # x = 30, y = 20 will be printed