0

defaultdict of defaultdict can use like this:(from Python: defaultdict of defaultdict at stackoverflow)

defaultdict(lambda : defaultdict(int))

Now,I want to use a defaultdict of OrderedDict,if I use like this,it will get error(TypeError: 'type' object is not iterable)

# tickets: List[List[str]]
# eg: [['a','b'],['c','d']]
m = defaultdict(lambda :OrderedDict(int))
for x, y in sorted(tickets):
    m[x][y] += 1

So I have to use setdefault.

m = defaultdict(lambda :OrderedDict())
for x, y in sorted(tickets):
    m[x].setdefault(y, 0)
    m[x][y] += 1

How can I write just like the defaultdict(lambda : defaultdict(int)) ? I want to make my code more concise.(it means not use setdefault) Thanks~

Community
  • 1
  • 1
hrwhisper
  • 115
  • 1
  • 7
  • @tobias_k I know that, must I use 'setdefault' for it? – hrwhisper Feb 04 '16 at 14:04
  • Not clear what you are asking: Do you want a defaultdict of ordereddict, or a defaultdict of _ordereddict-that-also-is-a-defaultdict_? – tobias_k Feb 04 '16 at 14:33
  • @tobias_k Thanks for reply~ I want to a defaultdict of ordereddict-that-also-is-a-defaultdict , maybe I should create a class to do that: [an ordered default dict in python](http://stackoverflow.com/questions/6190331/can-i-do-an-ordered-default-dict-in-python) – hrwhisper Feb 04 '16 at 15:07
  • In this case, I think it really is a duplicate of that other question. – tobias_k Feb 04 '16 at 15:12

0 Answers0