I have a dict that looks like this
db = {
'ObjectID': ['-1', '6', '10', '13', '13', '13', '-1', '-1', '-1', '-1', '-1', '-1'],
'Test_value': ['25', '0,28999999', '100,00000000', 'Geometry', '126641,847400000000', '473106,185600000030', ' ', ' ', ' ', ' ', ' ', ' '],
'Has_error': ['true', 'true', 'true', 'true', 'true', 'true', 'false', 'false', 'false', 'false', 'false', 'false'],
'Message': ['Table row counts are different', 'ObjectID 6 is different for Field DIKTE_BRUGDEK', 'ObjectID 10 is different for Field RICHTING_1', 'ObjectID 13 is different for Field GEOMETRIE', 'ObjectID 13 is different for Field X', 'ObjectID 13 is different for Field Y', 'Shape types are the same', 'Feature types are the same', 'Feature class extents are the same', 'GeometryDefs are the same', 'Field properties are the same', 'Spatial references are the same'], 'Identifier': ['Table', 'FeatureClass', 'FeatureClass', 'FeatureClass', 'FeatureClass', 'FeatureClass', 'FeatureClass', 'FeatureClass', 'FeatureClass', 'GeometryDef', 'Field', 'SpatialReference'],
'Base_value': ['23', '0,19000000', '394,00000000', 'Geometry', '126530,700000000000', '473095,700000000010', ' ', ' ', ' ', ' ', ' ', ' ']}
I want to to break it down into a smaller subset based on the entries in the list of ‘ObjectID’, namely -1.
My first attempt was to built an index of the values, like:
filter_ind = []
for k,v in db.iteritems():
for i in xrange(len(v)):
if (k == 'ObjectID') and (int(v[i]) != -1):
filter_ind.append(i)
Then I tried to build a new dict, using filter_ind as a sort filter:
dict((k,v[i]) for i in filter_ind for k, v in db.iteritems())
What I get is only the last match as v isn’t a list anymore:
{'ObjectID':'13','Test_value':'473106,185600000030','Has_error':'true',
'Message':'ObjectID 13 is different for Field Y',
'Identifier':'FeatureClass','Base_value': '473095,700000000010'}
Question: is there another way to filter a dict based on certain value within itself? If this is considered a relatively straigh forward approach, what is a smart way to use the index as filter to create a new dict? Thanks already.
I think you’re overcomplicating this a bit. First, there’s no need for the nested loops. You can get the indices you want this way:
Or more tersely,
Then you could use the ids to filter the individual lists: