Python: counting a numpy array with a large list, where the sequence value

I need to find in which index on the big list where it matches the sub-list.

c = np.array(close)
EMA50 = np.array(MA50)
sublist = [False,True,True]
biglist = (c-EMA50)/c>0.01
>>>array([False, False, False, False, False, False, False, False, False,
       False, False, False, False,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True, False, False,  True, False,  True,  True, False, False,
        True, False, False, False, False, False, False, False,  True,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False,  True,  True,  True,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False,  True,  True,  True,  True], dtype=bool)
>>>sublist in biglist
>>>False

I was expecting True, but returned False.

Desired Result:

index_loc = [12,31,68,112] 
+4
source share
2 answers

inwill not check subarrays. Instead, it checks the elements.

You will need to do something like this:
(Use Afor a large array and bfor subscriptions for readability.)

n = len(b)
c = [i for i in xrange(len(A)-n+1) if (b==A[i:i+n]).all()]

c - the required list of indices.

Explanation:
This is a basic understanding of lists in python.
The idea is to create sub-arrays of a large array and check if it matches the sublist.

:

c = []    
for i in xrange(len(A)-n+1):
    if (b==A[i:i+n]).all():    # if list and arrays match
        c.append(i)
0

sublist slicing -

np.flatnonzero(~a[:-2] & a[1:-1] & a[2:]) # a as data array

, - , 0- , , , 1- , . , sublist, - [False, True, True]. , False, , True. NumPy ~. , , np.flatnonzero.

-

In [79]: np.flatnonzero(~a[:-2] & a[1:-1] & a[2:])
Out[79]: array([ 12,  31,  68, 112])
+2

Source: https://habr.com/ru/post/1669121/


All Articles