collections

assertAlmostEqual in Python unit-test for collections of floats

assertAlmostEqual in Python unit-test for collections of floats Question: The assertAlmostEqual(x, y) method in Python’s unit testing framework tests whether x and y are approximately equal assuming they are floats. The problem with assertAlmostEqual() is that it only works on floats. I’m looking for a method like assertAlmostEqual() which works on lists of floats, sets …

Total answers: 12

Pythonic way to iterate over a collections.Counter() instance in descending order?

Pythonic way to iterate over a collections.Counter() instance in descending order? Question: In Python 2.7, I want to iterate over a collections.Counter instance in descending count order. >>> import collections >>> c = collections.Counter() >>> c[‘a’] = 1 >>> c[‘b’] = 999 >>> c Counter({‘b’: 999, ‘a’: 1}) >>> for x in c: print x …

Total answers: 3

setting the default string value of Python's collections.defaultdict

setting the default string value of Python's collections.defaultdict Question: I am using Python 3.2.3 and want to change the default returned string value: from collections import defaultdict d=defaultdict(str) d[“NonExistent”] The value returned is ”. How can I change this so that when a key is not found, “unknown” is returned instead of the empty string? …

Total answers: 1

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

Python "set" with duplicate/repeated elements

Python "set" with duplicate/repeated elements Question: Is there a standard way to represent a “set” that can contain duplicate elements. As I understand it, a set has exactly one or zero of an element. I want functionality to have any number. I am currently using a dictionary with elements as keys, and quantity as values, …

Total answers: 8

Accessing items in an collections.OrderedDict by index

Accessing items in an collections.OrderedDict by index Question: Lets say I have the following code: import collections d = collections.OrderedDict() d[‘foo’] = ‘python’ d[‘bar’] = ‘spam’ Is there a way I can access the items in a numbered manner, like: d(0) #foo’s Output d(1) #bar’s Output Asked By: Billjk || Source Answers: If its an …

Total answers: 9

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

Why does Python not support record type? (i.e. mutable namedtuple)

Why does Python not support record type? (i.e. mutable namedtuple) Question: Why does Python not support a record type natively? It’s a matter of having a mutable version of namedtuple. I could use namedtuple._replace. But I need to have these records in a collection and since namedtuple._replace creates another instance, I also need to modify …

Total answers: 11

defaultdict of defaultdict?

defaultdict of defaultdict? Question: Is there a way to have a defaultdict(defaultdict(int)) in order to make the following code work? for x in stuff: d[x.a][x.b] += x.c_int d needs to be built ad-hoc, depending on x.a and x.b elements. I could use: for x in stuff: d[x.a,x.b] += x.c_int but then I wouldn’t be able …

Total answers: 6