python - How to efficiently reduce a list: Entries which are not contained in other entries -


i'm new python. code reducing list of strings takes long time execute. functions find: strings in list aren't partial matches of other strings in same list. there more efficient form of code?

the code below seemed work better following: any(item1 item in my_list1 if item1.startswith(item1) or item1.endswith(item1)) related question (python list lookup partial match). using wrong?

right now, can find partial matches in my_list1 begin or end other entries in my_list1. i'd find partial matches, center matches.

#my_list1 be: my_list=['abcd', 'abcde', 'abcdef', 'bcd', 'bcde', 'bcdef']  item1 in my_list1:     icount=0         item2 in my_list1:         if item2.startswith(item1):              icount+=1         if icount>1:             break     if icount==1:        my_list2.append(item1)        print item1 

desired my_list2 be: ['abcdef']

when change line

if item2.startswith(item1): 

to

if item2 in item1: 

i go having thousands of results in my_list2 few redundancies 0 results in my_list2

you sort list length of entries prior searching it. way, don't need search entire list partial matches iterate on each entry, since know entries prior current entry won't partial match, because they're short. this:

l = ['abcd', 'abcde', 'abcdef', 'bcd', 'bcde', 'bcdef'] s_l = sorted(l, key=len) print("sorted list {}".format(s_l) out = [val i,val in enumerate(s_l)          if not any(val in ent ent in s_l[i+1:])] print out 

output:

sorted list ['bcd', 'abcd', 'bcde', 'abcde', 'bcdef', 'abcdef'] ['abcdef'] 

this piece might confusing:

if not any(val in ent ent in s_l[i+1:]) 

it's iterating on indices after current index (represented s_l[i+1:]), , checking see if val substring contained in of strings @ each index (represented val in ent). if of indices return true val in ent test, any call return true. we're saying, add val our out list if val not sub-string of of strings contained in s_l, starting after current s_l index.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -