building a dictionary dynamically

Discussion in 'Python' started by noydb, Feb 4, 2012.

  1. noydb

    noydb Guest

    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!
     
    noydb, Feb 4, 2012
    #1
    1. Advertising

  2. On Feb 4, 6: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!


    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.
     
    Richard Thomas, Feb 5, 2012
    #2
    1. Advertising

  3. noydb

    noydb Guest

    Ahh, I see now, thanks!
     
    noydb, Feb 5, 2012
    #3
  4. noydb

    noydb Guest

    Adding to dictionaries just isn't obvious... if it's not dict.Add or
    dict.Appaned or the like, not obvious to inexperienced me!
     
    noydb, Feb 6, 2012
    #4
  5. noydb

    alex23 Guest

    On Feb 5, 4:13 am, noydb <> 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.
     
    alex23, Feb 7, 2012
    #5
  6. noydb

    Dave Angel Guest

    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
     
    Dave Angel, Feb 7, 2012
    #6
  7. noydb

    Terry Reedy Guest

    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
     
    Terry Reedy, Feb 7, 2012
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    6
    Views:
    459
    Ilias Lazaridis
    Feb 21, 2006
  2. james_027
    Replies:
    1
    Views:
    342
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  3. Navkirat Singh
    Replies:
    6
    Views:
    3,182
    Navkirat Singh
    Jul 29, 2010
  4. Chris Rebert
    Replies:
    0
    Views:
    545
    Chris Rebert
    Jul 29, 2010
  5. Fox

    dictionary within dictionary

    Fox, Mar 8, 2005, in forum: ASP General
    Replies:
    5
    Views:
    199
    Michael D. Kersey
    Mar 13, 2005
Loading...

Share This Page