Python过滤器元组具有不同的大小

时间:2016-03-24 21:48:06

标签: python-2.7 filter

我试图过滤这个元组列表:

listTuples = [(50,), (60,), (70,), (120,), (50, 50), (60, 50), (70, 50), (120, 50), (50, 50,60), (60, 50, 60), (70, 50, 60), (120, 50, 60), (50, 50, 60, 70), (60, 50, 60,70), (70, 50, 60, 70), (120, 50, 60, 70)]

过滤器会检查元组的总和是否大于100

如何使用filter在python中执行此操作?

1 个答案:

答案 0 :(得分:1)

使用内置lambda

sum函数
>>> listTuples = [(50,), (60,), (70,), (120,), (50, 50), (60, 50), (70, 50), (120, 50), (50, 50,60), (60, 50, 60), (70, 50, 60), (120, 50, 60), (50, 50, 60, 70), (60, 50, 60,70), (70, 50, 60, 70), (120, 50, 60, 70)]
>>> filter(lambda x:sum(x)>100,listTuples)
[(120,), (60, 50), (70, 50), (120, 50), (50, 50, 60), (60, 50, 60), (70, 50, 60), (120, 50, 60), (50, 50, 60, 70), (60, 50, 60, 70), (70, 50, 60, 70), (120, 50, 60, 70)]
>>>