How to find substrings in a list of words












0















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










share|improve this question

























  • @balki d = ['GGBASDEPINK','ASDEKNIP','PINK','WORDRRAB','BAR' ,'KNIP', 'RAB']

    – LordNord
    Nov 21 '18 at 18:41
















0















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










share|improve this question

























  • @balki d = ['GGBASDEPINK','ASDEKNIP','PINK','WORDRRAB','BAR' ,'KNIP', 'RAB']

    – LordNord
    Nov 21 '18 at 18:41














0












0








0








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










share|improve this question
















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







python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 18:36









Austin

12.6k31031




12.6k31031










asked Nov 21 '18 at 18:22









LordNordLordNord

707




707













  • @balki d = ['GGBASDEPINK','ASDEKNIP','PINK','WORDRRAB','BAR' ,'KNIP', 'RAB']

    – LordNord
    Nov 21 '18 at 18:41



















  • @balki d = ['GGBASDEPINK','ASDEKNIP','PINK','WORDRRAB','BAR' ,'KNIP', 'RAB']

    – LordNord
    Nov 21 '18 at 18:41

















@balki d = ['GGBASDEPINK','ASDEKNIP','PINK','WORDRRAB','BAR' ,'KNIP', 'RAB']

– LordNord
Nov 21 '18 at 18:41





@balki d = ['GGBASDEPINK','ASDEKNIP','PINK','WORDRRAB','BAR' ,'KNIP', 'RAB']

– LordNord
Nov 21 '18 at 18:41












1 Answer
1






active

oldest

votes


















1














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']





share|improve this answer
























  • "for x, y in permutations(d, 2):" What does mean the number 2?

    – LordNord
    Nov 21 '18 at 18:40






  • 1





    For long lists, you will save a lot of work using combinations and check shorter in longer.

    – schwobaseggl
    Nov 21 '18 at 18:40











  • @schwobaseggl, I believe that doesn't test every words, since it's shorter like you say, ofcourse it works in this example.

    – Austin
    Nov 21 '18 at 18:42













  • @LordNord, Taking 2 words at a time.

    – Austin
    Nov 21 '18 at 18:42






  • 1





    Combinations catches all pairs once. Permutations catches all pairs twice. But only the shorter of the two involved strings can be a substring of the other, so one comparison is spurious. But for pairs the gain is marginal, as the iterations will just half while you add a comparison of lengthes.

    – schwobaseggl
    Nov 21 '18 at 18:44














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418338%2fhow-to-find-substrings-in-a-list-of-words%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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']





share|improve this answer
























  • "for x, y in permutations(d, 2):" What does mean the number 2?

    – LordNord
    Nov 21 '18 at 18:40






  • 1





    For long lists, you will save a lot of work using combinations and check shorter in longer.

    – schwobaseggl
    Nov 21 '18 at 18:40











  • @schwobaseggl, I believe that doesn't test every words, since it's shorter like you say, ofcourse it works in this example.

    – Austin
    Nov 21 '18 at 18:42













  • @LordNord, Taking 2 words at a time.

    – Austin
    Nov 21 '18 at 18:42






  • 1





    Combinations catches all pairs once. Permutations catches all pairs twice. But only the shorter of the two involved strings can be a substring of the other, so one comparison is spurious. But for pairs the gain is marginal, as the iterations will just half while you add a comparison of lengthes.

    – schwobaseggl
    Nov 21 '18 at 18:44


















1














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']





share|improve this answer
























  • "for x, y in permutations(d, 2):" What does mean the number 2?

    – LordNord
    Nov 21 '18 at 18:40






  • 1





    For long lists, you will save a lot of work using combinations and check shorter in longer.

    – schwobaseggl
    Nov 21 '18 at 18:40











  • @schwobaseggl, I believe that doesn't test every words, since it's shorter like you say, ofcourse it works in this example.

    – Austin
    Nov 21 '18 at 18:42













  • @LordNord, Taking 2 words at a time.

    – Austin
    Nov 21 '18 at 18:42






  • 1





    Combinations catches all pairs once. Permutations catches all pairs twice. But only the shorter of the two involved strings can be a substring of the other, so one comparison is spurious. But for pairs the gain is marginal, as the iterations will just half while you add a comparison of lengthes.

    – schwobaseggl
    Nov 21 '18 at 18:44
















1












1








1







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']





share|improve this answer













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']






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 18:35









AustinAustin

12.6k31031




12.6k31031













  • "for x, y in permutations(d, 2):" What does mean the number 2?

    – LordNord
    Nov 21 '18 at 18:40






  • 1





    For long lists, you will save a lot of work using combinations and check shorter in longer.

    – schwobaseggl
    Nov 21 '18 at 18:40











  • @schwobaseggl, I believe that doesn't test every words, since it's shorter like you say, ofcourse it works in this example.

    – Austin
    Nov 21 '18 at 18:42













  • @LordNord, Taking 2 words at a time.

    – Austin
    Nov 21 '18 at 18:42






  • 1





    Combinations catches all pairs once. Permutations catches all pairs twice. But only the shorter of the two involved strings can be a substring of the other, so one comparison is spurious. But for pairs the gain is marginal, as the iterations will just half while you add a comparison of lengthes.

    – schwobaseggl
    Nov 21 '18 at 18:44





















  • "for x, y in permutations(d, 2):" What does mean the number 2?

    – LordNord
    Nov 21 '18 at 18:40






  • 1





    For long lists, you will save a lot of work using combinations and check shorter in longer.

    – schwobaseggl
    Nov 21 '18 at 18:40











  • @schwobaseggl, I believe that doesn't test every words, since it's shorter like you say, ofcourse it works in this example.

    – Austin
    Nov 21 '18 at 18:42













  • @LordNord, Taking 2 words at a time.

    – Austin
    Nov 21 '18 at 18:42






  • 1





    Combinations catches all pairs once. Permutations catches all pairs twice. But only the shorter of the two involved strings can be a substring of the other, so one comparison is spurious. But for pairs the gain is marginal, as the iterations will just half while you add a comparison of lengthes.

    – schwobaseggl
    Nov 21 '18 at 18:44



















"for x, y in permutations(d, 2):" What does mean the number 2?

– LordNord
Nov 21 '18 at 18:40





"for x, y in permutations(d, 2):" What does mean the number 2?

– LordNord
Nov 21 '18 at 18:40




1




1





For long lists, you will save a lot of work using combinations and check shorter in longer.

– schwobaseggl
Nov 21 '18 at 18:40





For long lists, you will save a lot of work using combinations and check shorter in longer.

– schwobaseggl
Nov 21 '18 at 18:40













@schwobaseggl, I believe that doesn't test every words, since it's shorter like you say, ofcourse it works in this example.

– Austin
Nov 21 '18 at 18:42







@schwobaseggl, I believe that doesn't test every words, since it's shorter like you say, ofcourse it works in this example.

– Austin
Nov 21 '18 at 18:42















@LordNord, Taking 2 words at a time.

– Austin
Nov 21 '18 at 18:42





@LordNord, Taking 2 words at a time.

– Austin
Nov 21 '18 at 18:42




1




1





Combinations catches all pairs once. Permutations catches all pairs twice. But only the shorter of the two involved strings can be a substring of the other, so one comparison is spurious. But for pairs the gain is marginal, as the iterations will just half while you add a comparison of lengthes.

– schwobaseggl
Nov 21 '18 at 18:44







Combinations catches all pairs once. Permutations catches all pairs twice. But only the shorter of the two involved strings can be a substring of the other, so one comparison is spurious. But for pairs the gain is marginal, as the iterations will just half while you add a comparison of lengthes.

– schwobaseggl
Nov 21 '18 at 18:44






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418338%2fhow-to-find-substrings-in-a-list-of-words%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word