building a dictionary dynamically

N

noydb

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)


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!
 
R

Richard Thomas

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)


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[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.
 
N

noydb

Adding to dictionaries just isn't obvious... if it's not dict.Add or
dict.Appaned or the like, not obvious to inexperienced me!
 
A

alex23

How do you build a dictionary dynamically?

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.
 
D

Dave Angel

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)


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:
 
T

Terry Reedy

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top