views:

78

answers:

2

How do I use setdefault in python for nested dictionary structures. eg..

self.table[field] = 0
self.table[date] = []
self.table[value] = {}

I would like to setdefault for these.

A: 

Assuming self.table is a dict, you could use

self.table.setdefault(field,0)

The rest are all similar. Note that if self.table already has a key field, then the value associated with that key is returned. Only if there is no key field is self.table[field] set to 0.

Edit: Perhaps this is closer to what you want:

import collections
class Foo(object):
    def __init__(self):
        self.CompleteAnalysis=collections.defaultdict(
            lambda: collections.defaultdict(list))

    def getFilledFields(self,sentence):
        field, field_value, field_date = sentence.split('|')
        field_value = field_value.strip('\n')
        field_date = field_date.strip('\n')
        self.CompleteAnalysis[field]['date'].append(field_date)
        self.CompleteAnalysis[field]['value'].append(field_value) 

foo=Foo()
foo.getFilledFields('A|1|2000-1-1')
foo.getFilledFields('A|2|2000-1-2')
print(foo.CompleteAnalysis['A']['date'])
# ['2000-1-1', '2000-1-2']

print(foo.CompleteAnalysis['A']['value'])
# ['1', '2']

Instead of keeping track of the count, perhaps just take the length of the list:

print(len(foo.CompleteAnalysis['A']['value']))
# 2
unutbu
thanks one more query, how can I set self.table[tablename][field][count] = 0
Jayanth
TRying something like this, however not working. :-(( def getFilledFields(self,sentence): count = 0 date = [] value = [] field = sentence.split('|')[0] field_value = sentence.split('|')[1].strip('\n') field_date = sentence.split('|')[2].strip('\n') try: self.CompleteAnalysis[field][count] = self.CompleteAnalysis[field][count] + 1 except: self.CompleteAnalysis[field][count] = 0 self.CompleteAnalysis[field][date].append(field_date) self.CompleteAnalysis[field][value].append(field_value)
Jayanth
Could you add to your question what a sample sentence looks like and what you want self.CompleteAnalysis to look like after a call to `self.getFilledFields(sentence)`? I have a guess but am not quite sure.
unutbu
thanks that worked, how can I vote for you :-)
Jayanth
A: 

Not sure I understand what you are asking... but is this it?

>>> class c:
...     def __init__(self):
...         self.table = {}
...         self.table.setdefault('field', 0)
...         self.table.setdefault('date', [])
...         self.table.setdefault('value', {})
...
>>> v = c()
>>> print v.table
{'date': [], 'field': 0, 'value': {}}

I used the string 'field', 'date', 'value', which would obviously be replaced with variable data.

EDIT

>>> class c:
...     def __init__(self):
...         self.table = {}
...         fieldname = 'something'
...         self.table.setdefault(fieldname, {})
...         for i in xrange(0, 10):
...             self.table[fieldname].setdefault(i, 0)
...
>>> v = c()
>>> print v.table
{'something': {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}}

Is this what you mean?

sberry2A
thanks for the response, I just realised that I needed something more like self.table[fieldname][count] = 0, I wanted to set this by setdefault
Jayanth