2

私はこのように見える口述を持っています

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を作成しようとしました。 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をフィルタリングする別の方法はありますか?これが比較的単純なアプローチと見なされる場合、新しいdictを作成するためのフィルターとしてインデックスを使用するスマートな方法は何ですか?すでにありがとう。

4

6 に答える 6

4

あなたはこれを少し複雑にしすぎていると思います。まず、ネストされたループは必要ありません。この方法で必要なインデックスを取得できます。

oids = db['ObjectID']
for i, id in enumerate(oids):
    if id != -1
        filter_ind.append(i) 

またはもっと簡潔に、

filter_ind = [i for i, id in enumerate(oids) if id != '-1']

次に、IDを使用して個々のリストをフィルタリングできます。

dict((key, [val[i] for i in filter_ind]) for key, val in db.iteritems())
于 2012-07-11T16:26:44.617 に答える
2

これが私が作ったものです:

new_db=db.copy()
fltr=[x=='-1' for x in new_db['ObjectID']] #Not actually necessary, but makes the code a little more readable

for k,v in new_db.items():
    new_db[k]=[x for i,x in enumerate(new_db[k]) if fltr[i]]  #replace old lists with new filtered ones.

これは、senderleによって投稿された回答と非常によく似ています(私は思います)。私はブールリストを使用しますが、他の回答はインデックスを使用します。私のはおそらくそれほど効率的ではありませんが、私にとっては読みやすく、理解しやすいです。

于 2012-07-11T16:29:44.630 に答える
2

別のオプションは次のとおりです。

from operator import itemgetter

iget = itemgetter(*(i for i, id in enumerate(db['ObjectID']) if int(id) != -1))
result = dict((k, list(iget(v))) for k, v in db.items())
于 2012-07-11T16:34:18.590 に答える
1

2.7を使用している場合:

from itertools import compress
indexes = [(x != -1) for x in db['ObjectID']]
result = dict((k, compress(v, indexes)) for k, v in db.iteritems())
于 2012-07-11T16:39:02.233 に答える
1

これは実際に使用できるまれな機会ですitertools.compress

from itertools import compress

sels = [x != '-1' for x in db['ObjectID']]
comp = {key: list(compress(vals, sels)) for key, vals in db.items()}
于 2014-06-04T21:41:09.760 に答える
0

私はこれが好きです:

[dict(zip(db.keys(),e)) for e in zip(*db.values()) if e[0]!='-1']

-1の辞書を除いた辞書のリストを返します。

于 2012-07-11T17:10:37.740 に答える