0

I am trying a substring list with another list.The substring list looks like this:

area_index = ['sel_reg','sel_reg/clk1','top_mod/pcb/clk_reg','hello']

There is another list looks like this:

power_index = ['gpu_gpc_wrap/sel_reg/clk1','top_mod/pcb/clk_reg','gpu/gpu_gpc_wrap/sel_reg','top_mod/clk_bus']

I need to know if the element in area_index is a substring of one of the element in power_index. For example, 'sel_reg/clk1' is a substring of 'gpu_gpc_wrap/sel_reg/clk1' in power_index, while 'hello' is not a substring of any element in power_index. So i wrote a piece of code to solve the problem:

not_in = []
for gpc_name in area_index:
    find = False
    for p_name in power_index:
        if p_name.find(gpc_name) != -1:
            find = True
            power_index.remove(p_name)
            break
    if not find:
        not_in.append(gpc_name)
        
print(not_in)

Actually, it works. However, as the length of power_index or area_index increases, it becomes impossible for my computer to finish the task in a reasonable time.

What should I do to improve the performance?

Sayse
  • 42,633
  • 14
  • 77
  • 146
FunPlus
  • 97
  • 3
  • your code shows that you are removing elements from the power index, but you don't have that in your problem description. Can you say something about that? – Christian Sloper Jul 07 '21 at 13:27
  • Also check out the [second answer](https://stackoverflow.com/a/25102099/1324033) on the duplicate – Sayse Jul 07 '21 at 13:29
  • if i find a string in power_index corresponding to area_index, then i can remove it to shorten the length of power_index, which ease the burden of finding substrings afterwards. @ChristianSloper – FunPlus Jul 07 '21 at 15:50
  • those answers work, but the problem is that they run slowly. @Sayse – FunPlus Jul 08 '21 at 01:53

0 Answers0