Iterate over data to build dictionary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dshimer
    Recognized Expert New Member
    • Dec 2006
    • 136

    #1

    Iterate over data to build dictionary

    I have a file whose structure in strictly generic terms is similar to the following.
    Code:
    keyname first
    keyword1 1.1
    keyword2 1.2
    keyword3 1.3
    keyname second
    keyword1 2.1
    keyword2 2.2
    keyword3 2.3
    keyname third
    keyword1 3.1
    keyword2 3.2
    keyword3 3.3
    keyname value is always going to contain the name of a data set identified by keywords and values. The keywords are always the same for each keyname and contain data which is unique to the keyname. In the simplest terms if I read it like
    Code:
    >>> f=open('/tmp/test.txt','r')
    >>> d=f.readlines()
    >>> for l in d:
    ... 	print l.split()
    ... 
    ['keyname', 'first']
    ['keyword1', '1.1']
    ['keyword2', '1.2']
    ['keyword3', '1.3']
    ['keyname', 'second']
    ['keyword1', '2.1']
    ['keyword2', '2.2']
    ['keyword3', '2.3']
    ['keyname', 'third']
    ['keyword1', '3.1']
    ['keyword2', '3.2']
    ['keyword3', '3.3']
    What I would like to do is build a dictionary in which each keyname has a value which is another dictionary made up of keywords and values. For example if I were to manually build it the dictionary would look like
    Code:
    dict={'first':{'keyword1':1.1,'keyword2':1.2,'keyword3':1.3},'second':{'keyword1':2.1,'keyword2':2.2,'keyword3':2.3},'third':{'keyword1':3.1,'keyword2':3.2,'keyword3':3.3}}
    allowing for access to whole keys, or individual data values like
    Code:
    >>> dict['second']
    {'keyword3': 2.2999999999999998, 'keyword2': 2.2000000000000002, 'keyword1': 2.1000000000000001}
    >>> dict['second']['keyword2']
    2.2000000000000002
    This is ripe for iterating over the data and adding as I go, if it were a list I would append, but I don't use dictionaries very often and don't know how to add/append/insert data. What is the best way to do this?
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by dshimer
    I have a file whose structure in strictly generic terms is similar to the following.
    Code:
    keyname first
    keyword1 1.1
    keyword2 1.2
    keyword3 1.3
    keyname second
    keyword1 2.1
    keyword2 2.2
    keyword3 2.3
    keyname third
    keyword1 3.1
    keyword2 3.2
    keyword3 3.3
    keyname value is always going to contain the name of a data set identified by keywords and values. The keywords are always the same for each keyname and contain data which is unique to the keyname. In the simplest terms if I read it like
    Code:
    >>> f=open('/tmp/test.txt','r')
    >>> d=f.readlines()
    >>> for l in d:
    ... 	print l.split()
    ... 
    ['keyname', 'first']
    ['keyword1', '1.1']
    ['keyword2', '1.2']
    ['keyword3', '1.3']
    ['keyname', 'second']
    ['keyword1', '2.1']
    ['keyword2', '2.2']
    ['keyword3', '2.3']
    ['keyname', 'third']
    ['keyword1', '3.1']
    ['keyword2', '3.2']
    ['keyword3', '3.3']
    What I would like to do is build a dictionary in which each keyname has a value which is another dictionary made up of keywords and values. For example if I were to manually build it the dictionary would look like
    Code:
    dict={'first':{'keyword1':1.1,'keyword2':1.2,'keyword3':1.3},'second':{'keyword1':2.1,'keyword2':2.2,'keyword3':2.3},'third':{'keyword1':3.1,'keyword2':3.2,'keyword3':3.3}}
    allowing for access to whole keys, or individual data values like
    Code:
    >>> dict['second']
    {'keyword3': 2.2999999999999998, 'keyword2': 2.2000000000000002, 'keyword1': 2.1000000000000001}
    >>> dict['second']['keyword2']
    2.2000000000000002
    This is ripe for iterating over the data and adding as I go, if it were a list I would append, but I don't use dictionaries very often and don't know how to add/append/insert data. What is the best way to do this?
    It's simple:
    Code:
    aDict[newKeyName] = newValue
    Scary, huh?

    Comment

    • dshimer
      Recognized Expert New Member
      • Dec 2006
      • 136

      #3
      Good grief, I knew it had to be easy. I would be sorry for taking your time instead of digging through a book, but I guess it will be a good reference in case anybody else missed it. Python data types are just too cool and I'm finding that I like dictionaries more than I thought I would when I first started studying them.

      Thanks

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by dshimer
        Good grief, I knew it had to be easy. I would be sorry for taking your time instead of digging through a book, but I guess it will be a good reference in case anybody else missed it. Python data types are just too cool and I'm finding that I like dictionaries more than I thought I would when I first started studying them.

        Thanks
        Any time, D. It's really no trouble (and you contribute so much that the simplest to the toughest questions are yours for the asking). And as you say, it could help someone else along the way.

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by dshimer
          Good grief, I knew it had to be easy. I would be sorry for taking your time instead of digging through a book, but I guess it will be a good reference in case anybody else missed it. Python data types are just too cool and I'm finding that I like dictionaries more than I thought I would when I first started studying them.

          Thanks
          I like dictionaries also. I hope this helps you:
          Code:
          data = open(fn).read()
          dataLst = [i.strip() for i in data.split('keyname') if i != '']
          dd = {}
          for item in dataLst:
              itemLst = item.split('\n')
              dd[itemLst[0]] = dict(zip([i.split()[0] for i in itemLst[1:]], [j.split()[1] for j in itemLst[1:]]))
          
          for key in dd:
              print '%s = %s' % (key, dd[key])
          
          '''
          >>> second = {'keyword3': '2.3', 'keyword2': '2.2', 'keyword1': '2.1'}
          third = {'keyword3': '3.3', 'keyword2': '3.2', 'keyword1': '3.1'}
          first = {'keyword3': '1.3', 'keyword2': '1.2', 'keyword1': '1.1'}
          >>>
          '''
          I also apreciate your contributions. :)

          Comment

          Working...