【发布时间】:2014-09-25 18:26:46
【问题描述】:
我有一个像这样的字典
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', ' ', ' ', ' ', ' ', ' ', ' ']}
我想根据“ObjectID”列表中的条目将其分解为一个较小的子集,即-1。 我的第一次尝试是建立一个值的索引,例如:
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)
然后我尝试构建一个新的字典,使用 filter_ind 作为排序过滤器:
dict((k,v[i]) for i in filter_ind for k, v in db.iteritems())
我得到的只是最后一场比赛,因为 v 不再是一个列表:
{'ObjectID':'13','Test_value':'473106,185600000030','Has_error':'true',
'Message':'ObjectID 13 is different for Field Y',
'Identifier':'FeatureClass','Base_value': '473095,700000000010'}
问题:是否有另一种方法可以根据自身内部的特定值过滤 dict?如果这被认为是一种相对直接的方法,那么使用索引作为过滤器来创建新字典的聪明方法是什么?已经谢谢了。
【问题讨论】:
-
正如我所说,值列表应该只包含基于
db ['ObjectID'] != -1(即[1,2,3,4,5])索引的条目。 -
谢谢,我最初的解释是你想要一个字典列表,比如
[{'ObjectID': '6', ...}, {'ObjectID': '10', ...}, ...]。
标签: python dictionary filtering