Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 多条件过滤迭代_Python - Fatal编程技术网

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的所有迭代?

我想在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的所有迭代? 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)]
或者您可以使用内置的
过滤器