How to filter a dictionary according to an arbitrary condition function?
我有一本点字典,说:
1 | >>> points={'a':(3,4), 'b':(1,2), 'c':(5,5), 'd':(3,3)} |
我想创建一个新字典,所有x和y值小于5的点,即点"a"、"b"和"d"。
根据这本书,每个字典都有
1 2 | >>> points.items() [('a', (3, 4)), ('c', (5, 5)), ('b', (1, 2)), ('d', (3, 3))] |
所以我写了这个:
1 2 3 4 5 | >>> for item in [i for i in points.items() if i[1][0]<5 and i[1][1]<5]: ... points_small[item[0]]=item[1] ... >>> points_small {'a': (3, 4), 'b': (1, 2), 'd': (3, 3)} |
有更优雅的方式吗?我希望python有一些超级棒的
同样,在Python 2.7和镜头,你可以使用字典的理解:
1 | {k: v for k, v in points.iteritems() if v[0] < 5 and v[1] < 5} |
3:在Python
1 | {k: v for k, v in points.items() if v[0] < 5 and v[1] < 5} |
1 | dict((k, v) for k, v in points.items() if all(x < 5 for x in v)) |
你可以选择呼叫
如果你不知道,可能是过度的
1 | points_small = dict(filter(lambda (a,(b,c)): b<5 and c < 5, points.items())) |
1 2 3 4 | >>> points = {'a': (3, 4), 'c': (5, 5), 'b': (1, 2), 'd': (3, 3)} >>> dict(filter(lambda x: (x[1][0], x[1][1]) < (5, 5), points.items())) {'a': (3, 4), 'b': (1, 2), 'd': (3, 3)} |
1 | dict((k, v) for (k, v) in points.iteritems() if v[0] < 5 and v[1] < 5) |
我认为Alex Martelli’s的答案绝对是最优雅的方式来做,但只是想添加到您想要的方式satisfy超级真棒
1 2 3 4 5 6 7 8 9 10 11 | class FilterDict(dict): def __init__(self, input_dict): for key, value in input_dict.iteritems(): self[key] = value def filter(self, criteria): for key, value in self.items(): if (criteria(value)): self.pop(key) my_dict = FilterDict( {'a':(3,4), 'b':(1,2), 'c':(5,5), 'd':(3,3)} ) my_dict.filter(lambda x: x[0] < 5 and x[1] < 5) |
基本上,我们创建一个类,从
1 | dict((k, v) for (k, v) in points.iteritems() if v[0] < 5 and v[1] < 5) |