test_lib_sequences.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:sidekick 作者: fabiommendes 项目源码 文件源码
def test_reduceby(self):
        data = [1, 2, 3, 4, 5]

        def iseven(x): return x % 2 == 0
        assert reduceby(iseven, add, data, 0) == {False: 9, True: 6}
        assert reduceby(iseven, mul, data, 1) == {False: 15, True: 8}

        projects = [{'name': 'build roads', 'state': 'CA', 'cost': 1000000},
                    {'name': 'fight crime', 'state': 'IL', 'cost': 100000},
                    {'name': 'help farmers', 'state': 'IL', 'cost': 2000000},
                    {'name': 'help farmers', 'state': 'CA', 'cost': 200000}]
        assert reduceby(lambda x: x['state'],
                        lambda acc, x: acc + x['cost'],
                        projects, 0) == {'CA': 1200000, 'IL': 2100000}

        assert reduceby('state',
                        lambda acc, x: acc + x['cost'],
                        projects, 0) == {'CA': 1200000, 'IL': 2100000}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号