How to initialize defaultdict with keys?

From the comments, I’m assuming you want a dictionary that fits the following conditions: Is initialized with set of keys with an empty list value for each Has defaultdict behavior that can initialize an empty list for non-existing keys @Aaron_lab has the right method, but there’s a slightly cleaner way: d = defaultdict(list,{ k:[] for … Read more

TypeError: multiple bases have instance lay-out conflict

Instance lay-out conflict is a fancy way of saying that you’re trying to inherit from multiple–in this case built-in–types that cannot cooperate with each other quite well. More technically these classes have conflict in functionality of their common attributes. In this case both OrderedDict and defaultdict are two dictionary-like types with their own unique __setitem__ … Read more

Sorting a defaultdict by value in python

>>> sorted(cityPopulation.iteritems(),key=lambda (k,v): v[0],reverse=True) #1990 [(‘C’, [30, 10, 20]), (‘B’, [20, 30, 10]), (‘A’, [10, 20, 30])] >>> sorted(cityPopulation.iteritems(),key=lambda (k,v): v[2],reverse=True) #2010 [(‘A’, [10, 20, 30]), (‘C’, [30, 10, 20]), (‘B’, [20, 30, 10])] Note in python 3 you can’t automagically unpack lambda arguments so you would have to change the code sorted(cityPopulation.items(), key=lambda k_v: … Read more

Exposing `defaultdict` as a regular `dict`

defaultdict docs say for default_factory: If the default_factory attribute is None, this raises a KeyError exception with the key as argument. What if you just set your defaultdict’s default_factory to None? E.g., >>> d = defaultdict(int) >>> d[‘a’] += 1 >>> d defaultdict(<type ‘int’>, {‘a’: 1}) >>> d.default_factory = None >>> d[‘b’] += 2 Traceback … Read more

Can’t pickle defaultdict

In addition to Martijn’s explanation: A module-level function is a function which is defined at module level, that means it is not an instance method of a class, it’s not nested within another function, and it is a “real” function with a name, not a lambda function. So, to pickle your defaultdict, create it with … Read more

Format string unused named arguments [duplicate]

If you are using Python 3.2+, use can use str.format_map(). For bond, bond: from collections import defaultdict ‘{bond}, {james} {bond}’.format_map(defaultdict(str, bond=’bond’)) Result: ‘bond, bond’ For bond, {james} bond: class SafeDict(dict): def __missing__(self, key): return ‘{‘ + key + ‘}’ ‘{bond}, {james} {bond}’.format_map(SafeDict(bond=’bond’)) Result: ‘bond, {james} bond’ In Python 2.6/2.7 For bond, bond: from collections import … Read more

defaultdict with default value 1?

Short answer (as per Montaro’s answer below) defaultdict(lambda:1) Long answer on how defaultdicts work ht = {} ht = defaultdict(lambda:0, ht) defaultdicts are different from dict in that when you try to access a regular dict with a key that does not exists, it raises a KeyError. defaultdict, however, doesn’t raise an error: it creates … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)