Map keys to multiple values in a dictionary

 question 

  • Want a dictionary that maps keys to multiple values ​​(so-called multidict)

 

 solution 

  • A dictionary is an associative container where each key maps to a separate value. If you want the key to map to multiple values, you need to store the multiple values ​​in another container such as a list or set. For example, a dictionary might be created like this:
d = {
    'a' : [1, 2, 3],
    'b' : [4, 5],	
}

e = {
    'a' : [1, 2, 3],
    'b' : [4, 5],	
}

  

  • Whether to use a list or a set is entirely up to the application's intent. Use a list if you want to preserve the order in which elements were inserted. If you want to eliminate duplicate elements (and don't care about their order), use sets.
  • In order to easily create such a dictionary, the defaultdice class in the collections module can be used. A feature of defaultdict is that it automatically initializes the first value, so that you only need to focus on adding elements. E.g:
from collections import defaultdict

d = defaultdict(list)

d['a'].append(1)
d['a'].append(2)
d['b'].append(4)

print(d)
defaultdict(<class 'list'>, {'a': [1, 2], 'b': [4]})

from collections import defaultdict

d = defaultdict(set)

d['a'].add(1)
d['a'].add(2)
d['b'].add(4)

print(d)
defaultdict(<class 'set'>, {'a': {1, 2}, 'b': {4}})

  

  • One thing to note about defaultdict is that it automatically creates dictionary entries for later access (i.e. using those entries that are not currently found in the dictionary). If you don't want this feature, you can call setdefault() on a normal dictionary instead. E.g:
d = {}
d.setdefault('a', []).append(1)
d.setdefault('a', []).append(2)
d.setdefault('b', []).append(3)

print(d)

{'b': [3], 'a': [1, 2]}

  

  • However, many programmers find using setdefault() a bit unnatural -- not to mention creating a new instance with an initial value each time it is called (the empty list[] in the example).

 

 Discuss 

  • In principle, building a one-key, multi-value dictionary is easy. But if you try to initialize the first value yourself, it gets messy. For example, the following code might be written:
d = {}
for key, value in pairs:
    if key not in d:
        d[key] = []
    d[key].append(value)

  

  • The code will be much clearer after using defaultdict:
d = defaultdict(list)
for key, value in pairs:
    d[key].append(value)

  

  • The content of this section is strongly related to the problem of grouping records in data processing

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325578575&siteId=291194637