2016-03-24 37 views
0

我試圖篩選元組名單:Python的過濾器的元組不同尺寸

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

我怎麼能在python中使用filter來做到這一點?

回答

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)] 
>>> 
+0

謝謝!正是我所需要的 –

+0

歡迎:) – Copperfield