Reputation: 3294
I have a dictionary which defines the scheme for translating XML data by mapping certain keywords to certain XML element paths. The dictionary looks like this:
scheme = { 'phone' : 'People/Analyst/Phone', \
'email' : 'People/Analyst/Email', \
'address' : 'People/Analyst/Address' \
... and so on
I now have to accommodate a situation where a certain key might need to map to two or more element paths that will need to be concatenated. For example:
scheme = { 'phone' : 'People/Analyst/Phone', \
'email' : 'People/Analyst/Email', \
'address' : 'People/Analyst/Address' \
'name' : [ 'People/Analyst/FirstName', 'People/Analyst/FirstName', ' ' ]
... and so on
I figured that a list as a value in the dictionary for that scheme is the right way to do this. But what's the idiomatic way to go through such a dictionary? As someone relatively new to Python, I'd like to start doing things a little more pythonically. Right now I have something like this:
for k in scheme:
dataStore[k] = fetchXMLText(scheme[k])
But if an occasional value in scheme will be a list with two or more parameters and a trailing separator, what's the most idiomatic way to check for write this up? I imagine something like checking to see if scheme[k] is an instance of a list, and then going through that list individually. I can code that up easily, but is there some more elegant way than doing a conditional isinstance().... ?
Many thanks
Upvotes: 1
Views: 111
Reputation: 9879
I agree with @EmmettJ.Butler. To turn that into a complete answer:
Say you had:
scheme = { 'phone' : ['People/Analyst/Phone'], \
'email' : ['People/Analyst/Email'], \
'address': ['People/Analyst/Address'] \
'name' : [ 'People/Analyst/FirstName', 'People/Analyst/FirstName', ' ' ]
...
You could then iterate it through as follows:
for k,v in scheme.iteritems():
dataStore[k] = [fetchXMLText(path) for path in v]
Upvotes: 6