只要您在(a,b,c)上沒有任何重複項,您可以輸入由元組(a,b,c)索引的對象,並定義過濾器方法(可能是一個生成器)返回符合條件的所有條目。
class mydict(dict):
def filter(self,a=None, b=None, c=None):
for key,obj in enumerate(self):
if (a and (key[0] == a)) or not a:
if (b and (key[1] == b)) or not b:
if (c and (key[2] == c)) or not c:
yield obj
這是一個醜陋和非常低效的例子,但你明白了。我相信itertools中有更好的實現方法,或者其他的東西。
編輯:
我一直在想這件事。昨晚我玩弄了一些東西,想出了將這些對象存儲在一個列表中,並通過所需的關鍵字段存儲索引的字典。通過獲取所有指定條件的索引的交集來檢索對象。像這樣:
objs = []
aindex = {}
bindex = {}
cindex = {}
def insertobj(a,b,c,obj):
idx = len(objs)
objs.append(obj)
if a in aindex:
aindex[a].append(idx)
else:
aindex[a] = [idx]
if b in bindex:
bindex[b].append(idx)
else:
bindex[b] = [idx]
if c in cindex:
cindex[c].append(idx)
else :
cindex[c] = [idx]
def filterobjs(a=None,b=None,c=None):
if a : aset = set(aindex[a])
if b : bset = set(bindex[b])
if c : cset = set(cindex[c])
result = set(range(len(objs)))
if a and aset : result = result.intersection(aset)
if b and bset : result = result.intersection(bset)
if c and cset : result = result.intersection(cset)
for idx in result:
yield objs[idx]
class testobj(object):
def __init__(self,a,b,c):
self.a = a
self.b = b
self.c = c
def show(self):
print ('a=%i\tb=%i\tc=%s'%(self.a,self.b,self.c))
if __name__ == '__main__':
for a in range(20):
for b in range(5):
for c in ['one','two','three','four']:
insertobj(a,b,c,testobj(a,b,c))
for obj in filterobjs(a=5):
obj.show()
print()
for obj in filterobjs(b=3):
obj.show()
print()
for obj in filterobjs(a=8,c='one'):
obj.show()
它應該是相當快的,儘管對象在列表中,它們可以直接通過索引訪問。 「搜索」是在哈希字典上完成的。
@Martijn彼得斯給出了一個很好的例子,這個問題的匹配/選擇/過濾方面使用fnmatch在這裏的問題[鏈接](http://stackoverflow.com/questions/29638382/how-to-retrieve-dicts -a-list-of-dicts-using-wildcard-key-value)。 –
我喜歡這種方法 – shx2