I have some Python dictionaries like this:
A = {id: {idnumber: condition},....
e.g.
A = {1: {11 : 567.54}, 2: {14 : 123.13}, .....
I need to search if the dictionary has any idnumber == 11
and calculate something with the condition
. But if in the entire dictionary doesn't have any idnumber == 11
, I need to continue with the next dictionary.
This is my try:
for id, idnumber in A.iteritems():
if 11 in idnumber.keys():
calculate = ......
else:
break
You're close.
If you need to know how many
11
s there are as keys in the innerdict
s, you can:This works because a key can only be in each
dict
once so you just have to test if the key exits.in
returnsTrue
orFalse
which are equal to1
and0
, so thesum
is the number of occurences ofidnum
.dpath to the rescue.
http://github.com/akesterson/dpath-python
dpath lets you search by globs, which will get you what you want.
It will iterate out all of the conditions in the dictionary, so no special looping constructs required.
See also