简体   繁体   中英

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]

在此处输入图像描述

Is there common and elegant algorithms for that?

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:

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM