我有点词典,说:
>>> 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 回答
如今,在Python 2.7及更高版本中,您可以使用dict理解:
在Python 3中:
如果您使用的是Python 2,则可以选择调用
.iteritems()
而不是.items()
,points
可能有 lot 条目 .如果您确定每个点始终只是2D(在这种情况下您可能使用
and
表达相同的约束),那么all(x < 5 for x in v)
可能会有点过分但是它会正常工作;-) .我认为Alex Martelli的答案肯定是最优雅的方式,但只是想添加一种方法来满足你对Pythonic方式的超级真棒
dictionary.filter(f)
方法的需求:基本上我们创建一个继承自
dict
的类,但添加了filter方法 . 我们确实需要使用.items()
进行过滤,因为使用.iteritems()
而破坏性迭代会引发异常 .