如何将字典转换为默认的python字典?

如何将字典转换为默认的python字典?,python,dictionary,Python,Dictionary,因为我在使用字典,所以我得到了keyError,计划是将字典设置为defaultdict(int),这样在默认字典中,当出现键错误时,它将填充零。但我没有找到任何解决方案。如何在google中将字典转换为默认字典?请在这方面提供帮助我认为,键错误可以通过将0设置为默认值来解决 mapfile = { 1879048192: 0, 1879048193: 0, 1879048194: 0, 1879048195: 0, 1879048196: 4,

因为我在使用字典,所以我得到了keyError,计划是将字典设置为defaultdict(int),这样在默认字典中,当出现键错误时,它将填充零。但我没有找到任何解决方案。如何在google中将字典转换为默认字典?请在这方面提供帮助

我认为,
键错误
可以通过将
0
设置为默认值来解决

mapfile = {
    1879048192: 0,
    1879048193: 0,
    1879048194: 0,
    1879048195: 0,
    1879048196: 4,
    1879048197: 3,
    1879048198: 2,
    1879048199: 17,
    1879048200: 0,
    1879048201: 1,
    1879048202: 0,
    1879048203: 0,
    1879048204: 4,
    # intentionally missing byte
    1879048206: 2,
    1879048207: 1,
    1879048208: 0 # single byte cannot make up a dword
}

_buf = {}
for x in (x for x in mapfile.keys() if 0==x%4):
    try:
        s = "0x{0:02x}{1:02x}{2:02x}{3:02x}".format(mapfile[x+3],mapfile[x+2],mapfile[x+1],mapfile[x+0])
        print "offset ", x, " value ", s
        _buf[x] = int(s, 16)
    except KeyError as e:
        print "bad key ", e

print "_buf is ", _buf
希望这有帮助。您可以将代码更改为类似于
mapfile.get([x+3],0)


您可以使用
get(key,default)
,而不是重新创建字典
key
是要检索的键,
default
是如果该键不在字典中,则返回的值:

from collections import defaultdict
mydict = {1:4}
mydefaultdict = defaultdict(int, mydict)
>>>mydefaultdict[1]
4
>>>mydefaultdict[2]
0

您可以将词典转换为
defaultdict

my_dict = { }
print my_dict.get('non_existent_key', 0)
>> 0
mapfile = collections.defaultdict(int, {
    1879048192: 0,
    1879048193: 0,
    1879048194: 0,
    1879048195: 0,
    1879048196: 4,
    1879048197: 3,
    1879048198: 2,
    1879048199: 17,
    1879048200: 0,
    1879048201: 1,
    1879048202: 0,
    1879048203: 0,
    1879048204: 4,
    # intentionally missing byte
    1879048206: 2,
    1879048207: 1,
    1879048208: 0 # single byte cannot make up a dword
})

print(mapfile[1879048205])  # -> 0
print(mapfile['bogus'])  # -> 0
>a={1:0,2:1,3:0}
>>>从集合导入defaultdict
>>>defaultdict(int,a)
defaultdict(,{1:0,2:1,3:0})

尽管您可以通过以下方式将字典转换为
defaultdict

>>> a = {1:0, 2:1, 3:0}
>>> from collections import defaultdict
>>> defaultdict(int,a)
defaultdict(<type 'int'>, {1: 0, 2: 1, 3: 0})
在您的示例代码中,最好将其作为创建过程的一部分:

mapfile = collections.defaultdict(int, mapfile)
另一种选择是派生您自己的类。它不需要太多额外的代码来实现一个类似字典的类,该类不仅为缺少的键(如
defaultdict
s do)提供值,而且还对它们进行了一些健全性检查。这里有一个我的意思的例子-一个类似于
dict
-的类,它只接受缺少的键,如果它们是某种整数,而不是像常规的
defaultdict

my_dict = { }
print my_dict.get('non_existent_key', 0)
>> 0
mapfile = collections.defaultdict(int, {
    1879048192: 0,
    1879048193: 0,
    1879048194: 0,
    1879048195: 0,
    1879048196: 4,
    1879048197: 3,
    1879048198: 2,
    1879048199: 17,
    1879048200: 0,
    1879048201: 1,
    1879048202: 0,
    1879048203: 0,
    1879048204: 4,
    # intentionally missing byte
    1879048206: 2,
    1879048207: 1,
    1879048208: 0 # single byte cannot make up a dword
})

print(mapfile[1879048205])  # -> 0
print(mapfile['bogus'])  # -> 0

请您详细说明我的要求,我必须添加一个catch块?如果密钥不存在,您想做什么?谢谢。键丢失是随机的,不仅仅是对于(mapfile.get([x+3],0])。我想要的是,如果任何键丢失,那么在(mapfile.keys()中的x为x,如果0==x%4):#try:s=“0x{0:02x}{1:02x}{2:02x}{3:02x}格式(mapfile 3],mapfile[x+2],mapfile[x+1],mapfile[x+0],offset],“value”,s_buf[x]=int(s,16)#除了KeyError作为e:#u buf.get('non_existence_key',0)打印“_bufis”,_bufkeyerror:1879048215,因此您可以更改为
mapfile=defaultdict(int,mapfile)
在forloops上方。谢谢Martineau.mapfile=MyDefaultIntDict({1879048192:01879048193:01879048194:01879048195:01879048196:41879048197:31879048198:21879048199:171879048200:01879048201:119879048202:01879048203:01879048204:4,#故意丢失字节1879048206:2、1879048207:1、1879048208:0#yte不能组成dword})既然我必须这样做,因为我有大量的数据,我怎么能用一行替换它。这种方法正确吗?mapfile=MyDefaultIntDict(mapfile)是的,
mapfile=MyDefaultIntDict(mapfile)如果代码< > MpFrase最初是一个规则>代码> DICT。如果您觉得我的答案有用,请考虑接受和/或上投票(如果您能)。谢谢。马丁。有用的答案+1欢迎。也建议您阅读关于投票和接受答案的文章。