Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > building a dictionary dynamically

Reply
Thread Tools

building a dictionary dynamically

 
 
noydb
Guest
Posts: n/a
 
      02-04-2012
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!
 
Reply With Quote
 
 
 
 
Richard Thomas
Guest
Posts: n/a
 
      02-05-2012
On Feb 4, 6:13*pm, noydb <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
noydb
Guest
Posts: n/a
 
      02-05-2012
Ahh, I see now, thanks!
 
Reply With Quote
 
noydb
Guest
Posts: n/a
 
      02-06-2012
Adding to dictionaries just isn't obvious... if it's not dict.Add or
dict.Appaned or the like, not obvious to inexperienced me!
 
Reply With Quote
 
alex23
Guest
Posts: n/a
 
      02-07-2012
On Feb 5, 4:13*am, noydb <(E-Mail Removed)> 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.
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      02-07-2012
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

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      02-07-2012
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance ordered dictionary vs normal dictionary Navkirat Singh Python 6 07-29-2010 10:18 AM
Re: Performance ordered dictionary vs normal dictionary Chris Rebert Python 0 07-29-2010 06:11 AM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM
dictionary within dictionary Fox ASP General 5 03-13-2005 05:03 AM



Advertisments