defaultdict

Format string unused named arguments

Format string unused named arguments Question: Let’s say I have: action = ‘{bond}, {james} {bond}’.format(bond=’bond’, james=’james’) this wil output: ‘bond, james bond’ Next we have: action = ‘{bond}, {james} {bond}’.format(bond=’bond’) this will output: KeyError: ‘james’ Is there some workaround to prevent this error to happen, something like: if keyrror: ignore, leave it alone (but do …

Total answers: 9

Can't pickle defaultdict

Can't pickle defaultdict Question: I have a defaultdict that looks like this: dict1 = defaultdict(lambda: defaultdict(int)) The problem is, I can’t pickle it using cPickle. One of the solution that I found here is to use module-level function instead of a lambda. My question is, what is module-level function? How can I use the dictionary …

Total answers: 10

Exposing `defaultdict` as a regular `dict`

Exposing `defaultdict` as a regular `dict` Question: I am using defaultdict(set) to populate an internal mapping in a very large data structure. After it’s populated, the whole structure (including the mapping) is exposed to the client code. At that point, I don’t want anyone modifying the mapping. And nobody does, intentionally. But sometimes, client code …

Total answers: 3

python defaultdict: 0 vs. int and [] vs list

python defaultdict: 0 vs. int and [] vs list Question: Is there any difference between passing int and lambda: 0 as arguments? Or between list and lambda: []? It looks like they do the same thing: from collections import defaultdict dint1 = defaultdict(lambda: 0) dint2 = defaultdict(int) dlist1 = defaultdict(lambda: []) dlist2 = defaultdict(list) for …

Total answers: 1

Sorting a defaultdict by value in python

Sorting a defaultdict by value in python Question: I have a data-structure which is something like this: The population of three cities for different year are as follows. Name 1990 2000 2010 A 10 20 30 B 20 30 10 C 30 10 20 I am using a defaultdict to store the data. from collections …

Total answers: 4

Why is defaultdict creating an array for my values?

Why is defaultdict creating an array for my values? Question: I’m creating a defaultdict from an array of arrays: >>> array = [[‘Aaron’,’1′,’2′],[‘Ben’,’3′,’4′]] >>> d = defaultdict(list) >>> for i in array: … d[i[0]].append({"num1":i[1],"num2":i[2]}) My expected outcome is: >>> d defaultdict(<type ‘list’>, {‘Aaron’: {‘num1’: ‘1’, ‘num2’: ‘2’}, ‘Ben’: {‘num1’: ‘3’, ‘num2’: ‘4’}}) But my outcome …

Total answers: 3

Python defaultdict and lambda

Python defaultdict and lambda Question: In someone else’s code I read the following two lines: x = defaultdict(lambda: 0) y = defaultdict(lambda: defaultdict(lambda: 0)) As the argument of defaultdict is a default factory, I think the first line means that when I call x[k] for a nonexistent key k (such as a statement like v=x[k]), …

Total answers: 5

How does collections.defaultdict work?

Collections.defaultdict difference with normal dict Question: I’ve read the examples in python docs, but still can’t figure out what this method means. Can somebody help? Here are two examples from the python docs >>> from collections import defaultdict >>> s = ‘mississippi’ >>> d = defaultdict(int) >>> for k in s: … d[k] += 1 …

Total answers: 16

`if key in dict` vs. `try/except` – which is more readable idiom?

`if key in dict` vs. `try/except` – which is more readable idiom? Question: I have a question about idioms and readability, and there seems to be a clash of Python philosophies for this particular case: I want to build dictionary A from dictionary B. If a specific key does not exist in B, then do …

Total answers: 11