短代码:map,filter,lambda

时间:2014-04-13 19:49:54

标签: python lambda

我使用Python 3.4,我有这个代码:

result = []
    for i in r['resp']:
        for id in self.all_dicts:
            if i == id['id']:
                result.append(id)

它很长,所以我想缩短:

result = list(map(filter(lambda x: x == i,self.all_dicts),r['resp']))

但我有一个错误:

TypeError: 'filter' object is not callable

如何解决这个问题?感谢

1 个答案:

答案 0 :(得分:3)

我想你想要:

result = [id for id in self.dicts if id['id'] in r['resp']]