Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19926 > unrolled thread

building a dictionary dynamically

Started bynoydb <jenn.duerr@gmail.com>
First post2012-02-04 10:13 -0800
Last post2012-02-06 19:38 -0800
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  building a dictionary dynamically noydb <jenn.duerr@gmail.com> - 2012-02-04 10:13 -0800
    Re: building a dictionary dynamically Richard Thomas <chardster@gmail.com> - 2012-02-04 18:18 -0800
      Re: building a dictionary dynamically noydb <jenn.duerr@gmail.com> - 2012-02-05 13:10 -0800
        Re: building a dictionary dynamically noydb <jenn.duerr@gmail.com> - 2012-02-06 08:10 -0800
          Re: building a dictionary dynamically Terry Reedy <tjreedy@udel.edu> - 2012-02-07 02:03 -0500
    Re: building a dictionary dynamically Dave Angel <d@davea.name> - 2012-02-06 23:43 -0500
    Re: building a dictionary dynamically alex23 <wuwei23@gmail.com> - 2012-02-06 19:38 -0800

#19926 — building a dictionary dynamically

Fromnoydb <jenn.duerr@gmail.com>
Date2012-02-04 10:13 -0800
Subjectbuilding a dictionary dynamically
Message-ID<4ea33a86-a329-45e1-a765-0cdacd8de57f@m24g2000yqb.googlegroups.com>
How do you build a dictionary dynamically?  Doesn't seem to be an
insert object or anything.  So I need an empty dictionary that I then
want to populate with values I get from looping through a list and
grabbing some properties.  So simply, I have (fyi, arcpy = module for
interacting with gis data)

>>> inDict = {}
>>> for inFC in inFClist:
>>>     print inFC
>>>     inCount =  int(arcpy.GetCount_management(inFC).getOutput(0))


where I want to make a dictionary like {inFC: inCount, inFC:
inCount, ....}

How do I build this???

And, is dictionaries the best route go about doing a comparison, such
that in the end I will have two dictionaries, one for IN and one for
OUT, as in I'm moving data files and want to verify that the count in
each file matches between IN and OUT.

Thanks for any help!

[toc] | [next] | [standalone]


#19928

FromRichard Thomas <chardster@gmail.com>
Date2012-02-04 18:18 -0800
Message-ID<a833cd4c-1cd1-4970-a6b9-cb9c3a2b98e1@w4g2000vbc.googlegroups.com>
In reply to#19926
On Feb 4, 6:13 pm, noydb <jenn.du...@gmail.com> wrote:
> How do you build a dictionary dynamically?  Doesn't seem to be an
> insert object or anything.  So I need an empty dictionary that I then
> want to populate with values I get from looping through a list and
> grabbing some properties.  So simply, I have (fyi, arcpy = module for
> interacting with gis data)
>
> >>> inDict = {}
> >>> for inFC in inFClist:
> >>>     print inFC
> >>>     inCount =  int(arcpy.GetCount_management(inFC).getOutput(0))
>
> where I want to make a dictionary like {inFC: inCount, inFC:
> inCount, ....}
>
> How do I build this???
>
> And, is dictionaries the best route go about doing a comparison, such
> that in the end I will have two dictionaries, one for IN and one for
> OUT, as in I'm moving data files and want to verify that the count in
> each file matches between IN and OUT.
>
> Thanks for any help!

Dictionaries are mutable, you can modify them in place:

>>> myDict = {}
>>> for myKey in myList:
...     myDict[myKey] = doSomething(myKey)

Dictionaries sound like a good way to go. You only need the one
dictionary though, the IN one. When processing the outCount values you
can just check them against the inDict at that point.

Regards,
Chard.

[toc] | [prev] | [next] | [standalone]


#19933

Fromnoydb <jenn.duerr@gmail.com>
Date2012-02-05 13:10 -0800
Message-ID<6cbbbca9-49a6-4c8d-a00c-7ccfd24966d3@t30g2000vbx.googlegroups.com>
In reply to#19928
Ahh, I see now, thanks!

[toc] | [prev] | [next] | [standalone]


#19939

Fromnoydb <jenn.duerr@gmail.com>
Date2012-02-06 08:10 -0800
Message-ID<8c689454-157e-4b80-a1a0-41ae317ad207@i18g2000yqf.googlegroups.com>
In reply to#19933
Adding to dictionaries just isn't obvious... if it's not dict.Add or
dict.Appaned or the like, not obvious to inexperienced me!

[toc] | [prev] | [next] | [standalone]


#19949

FromTerry Reedy <tjreedy@udel.edu>
Date2012-02-07 02:03 -0500
Message-ID<mailman.5494.1328598245.27778.python-list@python.org>
In reply to#19939
On 2/6/2012 11:10 AM, noydb wrote:
> Adding to dictionaries just isn't obvious... if it's not dict.Add or
> dict.Appaned or the like, not obvious to inexperienced me!

Have you read section 4.8-mapping types, of the library manual?
Or help(dict) at the interactive prompt?

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#19934

FromDave Angel <d@davea.name>
Date2012-02-06 23:43 -0500
Message-ID<mailman.5491.1328589827.27778.python-list@python.org>
In reply to#19926
On 02/04/2012 01:13 PM, noydb wrote:
> How do you build a dictionary dynamically?  Doesn't seem to be an
> insert object or anything.  So I need an empty dictionary that I then
> want to populate with values I get from looping through a list and
> grabbing some properties.  So simply, I have (fyi, arcpy = module for
> interacting with gis data)
>
>>>> inDict = {}
>>>> for inFC in inFClist:
>>>>      print inFC
>>>>      inCount =  int(arcpy.GetCount_management(inFC).getOutput(0))
>
> where I want to make a dictionary like {inFC: inCount, inFC:
> inCount, ....}
>
> How do I build this???
>
> And, is dictionaries the best route go about doing a comparison, such
> that in the end I will have two dictionaries, one for IN and one for
> OUT, as in I'm moving data files and want to verify that the count in
> each file matches between IN and OUT.
>
> Thanks for any help!
A dictionary is a mapping from key to value.  So each entry consists of 
a key and a value, where the key is unique.  As soon as you add another 
item with the same key, it overwrites the earlier one.  The only other 
important constraint is that the key has to be of an immutable type, 
such as int or string.

So you want to add the current inFC:inCount as an item in the 
dictionary?  Put the following in your loop.
      inDict[inFC] = inCount


You might also want to check if the key is a duplicate, so you could 
warn your user, or something.  Easiest way to do that is
       if inFC in inDict:



-- 

DaveA

[toc] | [prev] | [next] | [standalone]


#19947

Fromalex23 <wuwei23@gmail.com>
Date2012-02-06 19:38 -0800
Message-ID<6e10da9a-a4e2-4531-8272-5f4fc4a592b9@g4g2000pbi.googlegroups.com>
In reply to#19926
On Feb 5, 4:13 am, noydb <jenn.du...@gmail.com> wrote:
> How do you build a dictionary dynamically?
> >>> inDict = {}
> >>> for inFC in inFClist:
> >>>     print inFC
> >>>     inCount =  int(arcpy.GetCount_management(inFC).getOutput(0))
>
> where I want to make a dictionary like {inFC: inCount, inFC:
> inCount, ....}
>
> How do I build this???

The easiest way is to use the standard dictionary constructor. dict()
can accept a list of key/value pairs:

    >>> pairs = [('a',1), ('b',2), ('c',3)]
    >>> dict(pairs)
        {'a': 1, 'c': 3, 'b': 2}

And one way to build a list is with a list comprehension:

    >>> pairs = [(key, i+1) for i, key in enumerate(['a','b','c'])]
    >>> pairs
    [('a', 1), ('b', 2), ('c', 3)]

So you can combine the two, using a list comprehension to build the
list that is passed into dict():

    >>> dict([(key, i+1) for i, key in enumerate(['a','b','c'])])
    {'a': 1, 'c': 3, 'b': 2}

As a convenience, you don't need the square brackets:

    >>> dict((key, i+1) for i, key in enumerate(['a','b','c']))
    {'a': 1, 'c': 3, 'b': 2}

For your example, I'd go with something like this:

    getCount = lambda x:
int(arcpy.GetCount_management(x).getOutput(0))
    inDict = dict(
        (inFC, getCount(inFC)) for inFC in inFClist
    )

Hope this helps.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web