2012-11-07 136 views
1
字典

源列表在Python過濾列表

[ 
    {'main_id': 0, '_id': ObjectId('111111111111111'), 'key1': 'val1'}, 
    {'main_id': 1, '_id': ObjectId('222222222222222'), 'key1': 'val2'}, 
    {'main_id': 1, '_id': ObjectId('333333333333333'), 'key1': 'val3'}, 
    {'main_id': 4, '_id': ObjectId('444444444444444'), 'key1': 'val4'}, 
    {'main_id': 2, '_id': ObjectId('555555555555555'), 'key1': 'val5'}, 
] 

字典

[ 
    {'main_id': 0, '_id': ObjectId('111111111111111'), 'key1': 'val1'}, 
    {'main_id': 1, '_id': ObjectId('222222222222222'), 'key1': 'val2'}, 
    {'main_id': 2, '_id': ObjectId('555555555555555'), 'key1': 'val3'}, 
] 

我想要得到詞典的新列表的過濾列表:

[ 
    {'main_id': 1, '_id': ObjectId('333333333333333'), 'key1': 'val3'}, 
    {'main_id': 4, '_id': ObjectId('444444444444444'), 'key1': 'val4'}, 
] 
換句話說

,我想要一個新的列表來包含過濾列表中不存在的值。 您的想法?

+2

在你的例子中,過濾的列表不是第一個列表的子集。爲什麼? – wim

+0

@wim相反,它是。 – pydsigner

+1

我認爲'val3'應該是過濾列表中的'val5'? –

回答

3

一個襯裏:

result = [s for s in original if s not in filtered] 

,或者使用filter

filter(lambda x: x not in filtered, original) 
2
full = [ 
    {'main_id': 0, '_id': ObjectId('111111111111111'), 'key1': 'val1'}, 
    {'main_id': 1, '_id': ObjectId('222222222222222'), 'key1': 'val2'}, 
    {'main_id': 1, '_id': ObjectId('333333333333333'), 'key1': 'val3'}, 
    {'main_id': 4, '_id': ObjectId('444444444444444'), 'key1': 'val4'}, 
    {'main_id': 2, '_id': ObjectId('555555555555555'), 'key1': 'val5'}, 
] 
filtered = [ 
    {'main_id': 0, '_id': ObjectId('111111111111111'), 'key1': 'val6'}, 
    {'main_id': 1, '_id': ObjectId('222222222222222'), 'key1': 'val7'}, 
    {'main_id': 2, '_id': ObjectId('555555555555555'), 'key1': 'val8'}, 
] 

diff = [x for x in full if x not in filtered] 
+0

最好先將「filtered」複製到一個集合中,因爲成員資格檢查會顯着加快。 (顯然,這取決於輸入的大小以及對性能/內存使用的需求)。 –

+0

啊,CPU與RAM。經典的決鬥。 – pydsigner

+0

但它不會讓你複製這個設置 - 將拋出字典obj是不可能的。 –

1

使用原始數據:

# dummy! 
ObjectId = str 


original = [ 
    {'main_id': 0, '_id': ObjectId('111111111111111'), 'key1': 'val1'}, 
    {'main_id': 1, '_id': ObjectId('222222222222222'), 'key1': 'val2'}, 
    {'main_id': 1, '_id': ObjectId('333333333333333'), 'key1': 'val3'}, 
    {'main_id': 4, '_id': ObjectId('444444444444444'), 'key1': 'val4'}, 
    {'main_id': 2, '_id': ObjectId('555555555555555'), 'key1': 'val5'}, 
] 

filtered = [ 
    {'main_id': 0, '_id': ObjectId('111111111111111'), 'key1': 'val1'}, 
    {'main_id': 1, '_id': ObjectId('222222222222222'), 'key1': 'val2'}, 
    {'main_id': 2, '_id': ObjectId('555555555555555'), 'key1': 'val3'}, 
] 

new_list = [d for d in original if d not in filtered] 

print(new_list)