我试图过滤这个元组列表:
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中执行此操作?
答案 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)]
>>>