How to convert a dictionary to a standard python dictionary?

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

Since I use a dictionary, I get a keyError, Plan should make the dictionary as defaultdict (int). Thus, in the default dictionary, it will be empty when there is a key error. But I did not find any solution. How can I convert a dictionary to a default dictionary in google? Please help with this.

+4
source share
4 answers

You can convert the dictionary to defaultdict:

>>> a = {1:0, 2:1, 3:0}
>>> from collections import defaultdict
>>> defaultdict(int,a)
defaultdict(<type 'int'>, {1: 0, 2: 1, 3: 0})
+6
source

Instead of re-creating the dictionary, you can use get(key, default). keyis the key you want to receive, and defaultis the value that will be returned if the key is not in the dictionary:

my_dict = { }
print my_dict.get('non_existent_key', 0)
>> 0
+3
source

, KeyError , 0 .

In [5]: mydict = {1:4}

In [6]: mydict.get(1, 0)
Out[6]: 4

In [7]: mydict.get(2, 0)
Out[7]: 0

, . - mapfile.get([x+3], 0).

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

defaultdict, :

mapfile = collections.defaultdict(int, mapfile)

:

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

Another alternative is to derive your own class. To implement a class like a dictionary, you don’t need a lot of additional code, which not only provided values ​​for the missing keys, for example, defaultdictdo, but also checked a little check on them. Here is an example of what I mean - the dict-like class , which only accepts missing keys if they are an integer, and not just something like normal defaultdict:

import numbers

class MyDefaultIntDict(dict):
    default_value = 0
    def __missing__(self, key):
        if not isinstance(key, numbers.Integral):
            raise KeyError('{!r} is an invalid key'.format(key))
        self[key] = self.default_value
        return self.default_value

mapfile = MyDefaultIntDict({
    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'])  # -> KeyError: "'bogus' is an invalid key"
0
source

Source: https://habr.com/ru/post/1599247/


All Articles