Reputation: 123
I'm trying to find if exists a substring in a list of strings, FOR EXAMPLE:
I have the list of words ['GGBASDEPINK','ASDEKNIP','PINK','WORDRRAB','BAR']
PINK is a substring of ASDEKNIP , because the reverse of PINK is KNIP and the word BAR is in WORDRRAB because the reverse is RAB
How to find if substrip is exits? and if yes so put in reverse that string so the new list should be:
d = ['GGBASDEPINK','ASDEKNIP','PINK','WORDRRAB','BAR' ,'KNIP', 'RAB']
I tried like this
d = ['GGBASDEPINK','ASDEKNIP','PINK','WORDRRAB','BAR']
for word in d:
word = word[::-1]
if word in d:
print(word)
But it gives nothing
Upvotes: 0
Views: 202
Reputation: 26039
Use itertools.permutations
:
from itertools import permutations
d = ['GGBASDEPINK','ASDEKNIP','PINK','WORDRRAB','BAR']
for x, y in permutations(d, 2):
rev = y[::-1]
if rev in x:
d.append(rev)
print(d)
# ['GGBASDEPINK', 'ASDEKNIP', 'PINK', 'WORDRRAB', 'BAR', 'KNIP', 'RAB']
Upvotes: 1