Search code examples
pythonalgorithmsortingfrequency-distribution

How to find the most frequent places in list for certain list of elements?


For example there is a list of elements

[8,6,3,7,1,8,8,9,2,0,5,4,7,9,2,8,2,5,5,6,3,0,1,7,9,2,9,6,7,0,5,2,7,4,5,6,2,1,9,0,3,1,3,9,4,9,2,7,5,9,0,5,2,1,8,6,4]

I'm trying to find places (indexes) inside above list to find hits of second

[1,4,3,8]

enter image description here

Is there common and elegant algorithms for that?


Solution

  • Here is a simple visual representation of it using a plot:

    import matplotlib.pyplot as plt
    
    lst = [8,6,3,7,1,8,8,9,2,0,5,4,7,9,2,8,2,5,5,6,3,0,1,7,9,2,9,6,7,0,5,2,7,4,5,6,2,1,9,0,3,1,3,9,4,9,2,7,5,9,0,5,2,1,8,6,4]
    target = [1,4,3,8]
    result = [0]*len(lst)
    
    for i in range(len(lst)):
        if lst[i] in target:
            result[i] = 1
    
    plt.step(range(len(lst)), result)
    plt.show()
    

    Giving the following, fairly easy to analyze result:

    enter image description here

    With the x-axis indicating the index in the list, and 1 represent in the target list and 0 not in it.