首页 文章

如何根据任意条件函数过滤字典?

提问于
浏览
151

我有点词典,说:

>>> points={'a':(3,4), 'b':(1,2), 'c':(5,5), 'd':(3,3)}

我想创建一个新的字典,其中包含x和y值小于5的所有点,即点'a','b'和'd' .

根据the book,每个字典都有 items() 函数,它返回 (key, pair) 元组列表:

>>> points.items()
[('a', (3, 4)), ('c', (5, 5)), ('b', (1, 2)), ('d', (3, 3))]

所以我写了这个:

>>> 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有一些非常棒的 dictionary.filter(f) 函数......

7 回答

  • 311

    如今,在Python 2.7及更高版本中,您可以使用dict理解:

    {k: v for k, v in points.iteritems() if v[0] < 5 and v[1] < 5}
    

    在Python 3中:

    {k: v for k, v in points.items() if v[0] < 5 and v[1] < 5}
    
  • 8
    dict((k, v) for k, v in points.items() if all(x < 5 for x in v))
    

    如果您使用的是Python 2,则可以选择调用 .iteritems() 而不是 .items()points 可能有 lot 条目 .

    如果您确定每个点始终只是2D(在这种情况下您可能使用 and 表达相同的约束),那么 all(x < 5 for x in v) 可能会有点过分但是它会正常工作;-) .

  • 18
    points_small = dict(filter(lambda (a,(b,c)): b<5 and c < 5, points.items()))
    
  • 6
    >>> 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)}
    
  • 9
    dict((k, v) for (k, v) in points.iteritems() if v[0] < 5 and v[1] < 5)
    
  • 7

    我认为Alex Martelli的答案肯定是最优雅的方式,但只是想添加一种方法来满足你对Pythonic方式的超级真棒 dictionary.filter(f) 方法的需求:

    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)
    

    基本上我们创建一个继承自 dict 的类,但添加了filter方法 . 我们确实需要使用 .items() 进行过滤,因为使用 .iteritems() 而破坏性迭代会引发异常 .

  • 105
    dict((k, v) for (k, v) in points.iteritems() if v[0] < 5 and v[1] < 5)
    

相关问题