I'm new to Python. My code for reducing a list of strings takes a long time to execute. It functions to find: only those strings in a list which aren't partial matches of other strings in the same list. Is there a more efficient form of this code?
The code below seemed to work better than the following: any(item1 for item in my_list1 if item1.startswith(item1) or item1.endswith(item1))
from a related question (Python list lookup with partial match). Am I using any wrong?
Right now, I can only find partial matches in my_list1 which begin or end other entries in my_list1. I'd like to find ALL partial matches, even center matches.
#My_list1 could be:
my_list=['abcd', 'abcde', 'abcdef', 'bcd', 'bcde', 'bcdef']
for item1 in my_list1:
icount=0
for 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 would be:
['abcdef']
When I change the line
if item2.startswith(item1):
to
if item2 in item1:
I go from having thousands of results in my_list2 with few redundancies to zero results in my_list2