Python 多条件过滤迭代
我想在Python中过滤匹配多个条件的特定迭代Python 多条件过滤迭代,python,Python,我想在Python中过滤匹配多个条件的特定迭代 from itertools import combinations iteration = combinations(range(5),3) print(list(iteration)) [(0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 2, 3), (0, 2, 4), (0, 3, 4), (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)] 如何过滤包含数字0和3的所有迭代?
from itertools import combinations
iteration = combinations(range(5),3)
print(list(iteration))
[(0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 2, 3), (0, 2, 4), (0, 3, 4), (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
如何过滤包含数字0和3的所有迭代?
ie:我想删除(0,1,3),(0,2,3),(0,3,4)命令式解决方案:
filteredList = []
for value in iteration:
if (0 not in value and 3 not in value):
filteredList.append(value)
filteredList = list(filter(lambda value: 0 not in value and 3 not in value, iteration))
过滤溶液:
filteredList = []
for value in iteration:
if (0 not in value and 3 not in value):
filteredList.append(value)
filteredList = list(filter(lambda value: 0 not in value and 3 not in value, iteration))
列出理解解决方案(谢谢@Sushanth):
您可以使用列表理解
[v代表v值,如果不是(0代表v,3代表v)]
或者您可以使用内置的过滤器