У меня есть функция python, которая принимает два списка, ищет пары на двух входах, где оба имеют положительные значения в одном и том же индексе, и создает два выходных списка, добавляя к каждому из них эти два положительных значения. У меня есть рабочая функция:Эффективно получать пары положительных значений с numpy
def get_pairs_in_first_quadrant(x_in, y_in):
"""If both x_in[i] and y_in[i] are > 0 then both will appended to the output list. If either are negative
then the pair of them will be absent from the output list.
:param x_in: A list of positive or negative floats
:param y_in: A list of positive or negative floats
:return: A list of positive floats <= in length to the inputs.
"""
x_filtered, y_filtered = [], []
for x, y in zip(x_in, y_in):
if x > 0 and y > 0:
x_filtered.append(x)
y_filtered.append(y)
return x_filtered, y_filtered
Как сделать это быстрее, используя numpy?
Используйте [numpy.logical_and] (http://docs.scipy.org/doc/numpy/reference/routines .logic.html). –
Насколько велики списки, о которых мы говорим здесь? – koukouviou
Длина может быть около 100 000. – ayeayeron