【发布时间】:2018-01-09 03:43:36
【问题描述】:
我正在使用 python 并尝试过滤二维数组以仅包含具有特定总和且不包含元素 0 的数组。
其他教程似乎展示了如何使用 numpy.where 过滤数组以获取满足条件的某些元素,但我试图只获取满足条件的某些数组,当然不使用循环,而是使用 numpy方法。
类似这个操作,但是使用数组和numpy:
import itertools
list_o_tuples = list(filter(lambda x: sum(x)==10 and 0 not in x,
itertools.combinations(range(10),3)))
#returns [(1, 2, 7), (1, 3, 6), (1, 4, 5), (2, 3, 5)]
【问题讨论】:
标签: python arrays numpy combinations itertools