RE: Dictionary of Dicts question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Townsend

    RE: Dictionary of Dicts question

    Joe had a good point! Let me describe what problem I'm trying to solve and the list can recommend some suggestions.

    I have two text files. Each file contains data like this:

    Test file 1234 4567 8975

    I want to compare the numbers in each text file. The data set (i.e. the numbers) has a unique identifier: the "test" + the "file". The text files are similar, but may not be exactly the same.

    My initial idea was to read the text files and turn each line into a dictionary entry. A dict for each text file. Then walk through the dicts and compare the numbers.

    If anyone has a better idea, I'd love to hear it.





    -----Original Message-----
    From: Joe Strout [mailto:joe@stro ut.net]
    Sent: Thursday, October 16, 2008 1:49 PM
    To: John Townsend
    Subject: Re: Dictionary of Dicts question

    On Oct 16, 2008, at 1:19 PM, John Townsend wrote:
    Accessing values is pretty straightforward (nice change from my Perl
    days). For example:
    >
    myDict['TestName']['FileName']['ct_shutdown']
    >
    in Python interpreter yields
    >
    9021
    FWIW, I'd recommend you scrap this dict-of-dicts structure and instead
    define a class (or three). You'll know you have it right when instead
    of the above, you're typing something like:

    myDict['TestName'].FileName.ct_sh utdown

    (The division might be a little different -- it's not entirely clear
    to me what in your sample code is just place-holder names, and what is
    meant literally.)
    However, when I try to add, let's say, a new FileName entry, I end
    up replacing the previous FileName entry.
    >
    In Python interpreter, I try:
    >
    myDict['TestName'] = {'NewFileName': {}, }
    Well, yes, this code says "stuff the new dictionary {'NewFileName': {}}
    into myDict under 'TestName', replacing whatever was under 'TestName'
    before."

    If that's not what you want, then don't do that -- assign to
    myDict['TestName2'] or some such.
    So, how do I add a new entry without replacing the old entry?
    You don't; that's the whole point of dictionaries. A dictionary maps
    keys to values. You can't have two values for the same key. You
    could have a key map to a list, but I suspect we're straying a bit far
    now from whatever it is you're trying to accomplish.

    If you can explain what it is you're actually trying to accomplish
    (without reference to implementation details like dictionaries and
    entries), maybe we can suggest a suitable approach.

    Best,
    - Joe

  • John Townsend

    #2
    RE: Dictionary of Dicts question

    Here are some sample lines.

    Text file 1 contains:

    DescribeImage AllAdjustments. psd 0.668000012636 0.046 0.426 0.06475 0.06475 0.005875
    DescribeImage All_Options_Mul ti.psd 0.552750021219 0.046 0.355875 0.01525 0.017125 0.0
    DescribeImage All_Options_Qua d.psd 0.57025000453 0.046 0.314875 0.058375 0.058375 0.007875
    DescribeImage Apple_RGB.psd 0.538999974728 0.046 0.315 0.04675 0.04875 0.0

    Text file 2 contains:
    DescribeImage AllAdjustments. psd 0.7889 0.056 0.786 0.0665 0.06476 0.999
    DescribeImage All_Options_Mul ti.psd 0.5527500421419 0.43154312 0.4443 0.43124 0.017125 0.0
    DescribeImage All_Options_Qua d.psd 0.5702503200453 0.046 0.34 0.058375 0.4342 0.43214

    Lines are tab delimited. Note, in this example text file 2 contains three lines, while text file 1 contains four lines.

    Where there are matching lines in each text file (e.g. "DescribeIm age AllAdjustments. psd" exists in both files), I want to compare each of the numbers from that line to the numbers in the corresponding line.

    If there is not corresponding line (like " DescribeImage Apple_RGB.psd") , skip the comparison test.

    I hope this helps describe my problem.

    -----Original Message-----
    From: python-list-bounces+jtownse n=adobe.com@pyt hon.org [mailto:python-list-bounces+jtownse n=adobe.com@pyt hon.org] On Behalf Of Dennis Lee Bieber
    Sent: Thursday, October 16, 2008 2:44 PM
    To: python-list@python.org
    Subject: Re: Dictionary of Dicts question

    On Thu, 16 Oct 2008 14:05:16 -0700, John Townsend <jtownsen@adobe .com>
    declaimed the following in comp.lang.pytho n:
    >
    I have two text files. Each file contains data like this:
    >
    Test file 1234 4567 8975
    >
    I want to compare the numbers in each text file. The data set (i.e. the numbers) has a unique identifier: the "test" + the "file". The text files are similar, but may not be exactly the same.
    >
    This would be easier if you gave a few lines sample from each file
    and the expected output of your processing of those lines.
    My initial idea was to read the text files and turn each line into a dictionary entry. A dict for each text file. Then walk through the dicts and compare the numbers.
    >
    If anyone has a better idea, I'd love to hear it.
    >
    Use a relational database and "GROUP BY" selections?
    --
    Wulfraed Dennis Lee Bieber KD6MOG
    wlfraed@ix.netc om.com wulfraed@bestia ria.com

    (Bestiaria Support Staff: web-asst@bestiaria. com)

    --

    Comment

    • MRAB

      #3
      Re: Dictionary of Dicts question

      On Oct 16, 11:03 pm, John Townsend <jtown...@adobe .comwrote:
      Here are some sample lines.
      >
      Text file 1 contains:
      >
      DescribeImage   AllAdjustments. psd      0.668000012636  0.046   0.426   0.06475 0.06475 0.005875
      DescribeImage   All_Options_Mul ti.psd   0.552750021219  0.046   0..355875        0.01525 0.017125        0.0
      DescribeImage   All_Options_Qua d.psd    0.57025000453   0.046  0.314875        0.058375        0.058375        0.007875
      DescribeImage   Apple_RGB.psd   0.538999974728  0.046   0.315  0.04675 0.04875 0.0
      >
      Text file 2 contains:
      DescribeImage   AllAdjustments. psd      0.7889  0.056   0.786  0.0665  0.06476 0.999
      DescribeImage   All_Options_Mul ti.psd   0.5527500421419 0.43154312      0.4443  0.43124 0.017125        0.0
      DescribeImage   All_Options_Qua d.psd    0.5702503200453 0.046   0..34    0.058375        0.4342  0.43214
      >
      Lines are tab delimited. Note, in this example text file 2 contains threelines, while text file 1 contains four lines.
      >
      Where there are matching lines in each text file (e.g. "DescribeIm age  AllAdjustments. psd" exists in both files), I want to compare each of the numbers from that line to the numbers in the corresponding line.
      >
      If there is not corresponding line (like " DescribeImage        Apple_RGB.psd") , skip the comparison test.
      >
      I hope this helps describe my problem.
      >
      If the first 2 fields are unique when combined, then you could use
      that as the key:

      data = {}
      for line in open(path):
      fields = line.split("\t" )
      data[tuple(fields[ : 2])] = fields[2 : ]

      Comment

      • bearophileHUGS@lycos.com

        #4
        Re: Dictionary of Dicts question

        MRAB:
        for line in open(path):
        fields = line.split("\t" )
        data[tuple(fields[ : 2])] = fields[2 : ]
        Keeping the key as a string may have some memory/performance
        advantages (not tested):

        for line in open(path):
        fields = line.split("\t" )
        data[fields[0] + fields[1]] = map(float, islice(fields, 2, None))

        Or probably faster (not tested):

        for line in open(path):
        parts = s.rsplit("\t", 6)
        data[parts[0]] = map(float, islice(parts, 1, None))

        Or (not tested):

        for line in open(path):
        parts = s.rsplit("\t", 6)
        data[parts[0]] = [float(parts[i]) for i in xrange(1, 7)]

        Having a built-in xsplit/xsplitr method here probably helps
        significantly.
        If the FP numbers are really precise then you can compare them as
        strings too, but that's quite unsafe.

        Bye,
        bearophile

        Comment

        • George Sakkis

          #5
          Re: Dictionary of Dicts question

          On Oct 16, 5:05 pm, John Townsend <jtown...@adobe .comwrote:
          Joe had a good point! Let me describe what problem I'm trying to solve and the list can recommend some suggestions.
          >
          I have two text files. Each file contains data like this:
          >
          Test file 1234 4567 8975
          >
          I want to compare the numbers in each text file. The data set (i.e. the numbers) has a unique identifier: the "test" + the "file". The text files are similar, but may not be exactly the same.
          >
          My initial idea was to read the text files and turn each line into a dictionary entry. A dict for each text file. Then walk through the dicts and compare the numbers.
          >
          If anyone has a better idea, I'd love to hear it.
          If getting the diffs is the only thing you want to do with the data,
          your idea is good enough. For more open-ended data manipulation you
          should move to a database like in Dennis's reply but for a single
          simple task a DB (even sqlite) is probably an overkill.

          Here's a quick solution using the dicts approach:

          import sys
          from operator import sub

          def read_data(path) :
          data = {}
          for line in open(path):
          fields = line.split('\t' )
          data[tuple(fields[:2])] = map(float,field s[2:])
          return data

          d1 = read_data(sys.a rgv[1])
          d2 = read_data(sys.a rgv[2])
          for key in d1:
          if key in d2:
          diffs = map(sub, d1[key], d2[key])
          print key, diffs

          HTH,
          George

          Comment

          Working...