How to find substrings in a list of words
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
@balki d = ['GGBASDEPINK','ASDEKNIP','PINK','WORDRRAB','BAR' ,'KNIP', 'RAB']
– LordNord
Nov 21 at 18:41
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
python python-3.x
edited Nov 21 at 18:36
Austin
8,8293828
8,8293828
asked Nov 21 at 18:22
LordNord
436
436
@balki d = ['GGBASDEPINK','ASDEKNIP','PINK','WORDRRAB','BAR' ,'KNIP', 'RAB']
– LordNord
Nov 21 at 18:41
add a comment |
@balki d = ['GGBASDEPINK','ASDEKNIP','PINK','WORDRRAB','BAR' ,'KNIP', 'RAB']
– LordNord
Nov 21 at 18:41
@balki d = ['GGBASDEPINK','ASDEKNIP','PINK','WORDRRAB','BAR' ,'KNIP', 'RAB']
– LordNord
Nov 21 at 18:41
@balki d = ['GGBASDEPINK','ASDEKNIP','PINK','WORDRRAB','BAR' ,'KNIP', 'RAB']
– LordNord
Nov 21 at 18:41
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
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']
"for x, y in permutations(d, 2):" What does mean the number 2?
– LordNord
Nov 21 at 18:40
1
For long lists, you will save a lot of work usingcombinationsand checkshorter in longer.
– schwobaseggl
Nov 21 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 at 18:42
@LordNord, Taking 2 words at a time.
– Austin
Nov 21 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 at 18:44
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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']
"for x, y in permutations(d, 2):" What does mean the number 2?
– LordNord
Nov 21 at 18:40
1
For long lists, you will save a lot of work usingcombinationsand checkshorter in longer.
– schwobaseggl
Nov 21 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 at 18:42
@LordNord, Taking 2 words at a time.
– Austin
Nov 21 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 at 18:44
|
show 1 more comment
up vote
1
down vote
accepted
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']
"for x, y in permutations(d, 2):" What does mean the number 2?
– LordNord
Nov 21 at 18:40
1
For long lists, you will save a lot of work usingcombinationsand checkshorter in longer.
– schwobaseggl
Nov 21 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 at 18:42
@LordNord, Taking 2 words at a time.
– Austin
Nov 21 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 at 18:44
|
show 1 more comment
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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']
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']
answered Nov 21 at 18:35
Austin
8,8293828
8,8293828
"for x, y in permutations(d, 2):" What does mean the number 2?
– LordNord
Nov 21 at 18:40
1
For long lists, you will save a lot of work usingcombinationsand checkshorter in longer.
– schwobaseggl
Nov 21 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 at 18:42
@LordNord, Taking 2 words at a time.
– Austin
Nov 21 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 at 18:44
|
show 1 more comment
"for x, y in permutations(d, 2):" What does mean the number 2?
– LordNord
Nov 21 at 18:40
1
For long lists, you will save a lot of work usingcombinationsand checkshorter in longer.
– schwobaseggl
Nov 21 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 at 18:42
@LordNord, Taking 2 words at a time.
– Austin
Nov 21 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 at 18:44
"for x, y in permutations(d, 2):" What does mean the number 2?
– LordNord
Nov 21 at 18:40
"for x, y in permutations(d, 2):" What does mean the number 2?
– LordNord
Nov 21 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 at 18:40
For long lists, you will save a lot of work using
combinations and check shorter in longer.– schwobaseggl
Nov 21 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 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 at 18:42
@LordNord, Taking 2 words at a time.
– Austin
Nov 21 at 18:42
@LordNord, Taking 2 words at a time.
– Austin
Nov 21 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 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 at 18:44
|
show 1 more comment
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
@balki d = ['GGBASDEPINK','ASDEKNIP','PINK','WORDRRAB','BAR' ,'KNIP', 'RAB']
– LordNord
Nov 21 at 18:41