Python: Check if string and its substring are existing in the same list2019 Community Moderator ElectionHow do I check if a list is empty?How do I check whether a file exists without exceptions?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonIs there a way to substring a string?Check if a given key already exists in a dictionaryHow to get the number of elements in a list in Python?How to concatenate two lists in Python?Does Python have a string 'contains' substring method?How to lowercase a string in Python?
Time travel from stationary position?
Is it true that good novels will automatically sell themselves on Amazon (and so on) and there is no need for one to waste time promoting?
A diagram about partial derivatives of f(x,y)
Do I need life insurance if I can cover my own funeral costs?
Why does overlay work only on the first tcolorbox?
Adventure Game (text based) in C++
Why no Iridium-level flares from other satellites?
ERC721: How to get the owned tokens of an address
A single argument pattern definition applies to multiple-argument patterns?
Is there a symmetric-key algorithm which we can use for creating a signature?
I am confused as to how the inverse of a certain function is found.
How to pronounce "I ♥ Huckabees"?
Why do passenger jet manufacturers design their planes with stall prevention systems?
What is "focus distance lower/upper" and how is it different from depth of field?
Employee lack of ownership
What is the adequate fee for a reveal operation?
Do I need to be arrogant to get ahead?
Is honey really a supersaturated solution? Does heating to un-crystalize redissolve it or melt it?
Meme-controlled people
How to make healing in an exploration game interesting
How to terminate ping <dest> &
Is there a hypothetical scenario that would make Earth uninhabitable for humans, but not for (the majority of) other animals?
Are Roman Catholic priests ever addressed as pastor
Most cost effective thermostat setting: consistent temperature vs. lowest temperature possible
Python: Check if string and its substring are existing in the same list
2019 Community Moderator ElectionHow do I check if a list is empty?How do I check whether a file exists without exceptions?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonIs there a way to substring a string?Check if a given key already exists in a dictionaryHow to get the number of elements in a list in Python?How to concatenate two lists in Python?Does Python have a string 'contains' substring method?How to lowercase a string in Python?
I've extracted keywords based on 1-gram, 2-gram, 3-gram within a tokenized sentence
list_of_keywords = []
for i in range(0, len(stemmed_words)):
temp = []
for j in range(0, len(stemmed_words[i])):
temp.append([' '.join(x) for x in list(everygrams(stemmed_words[i][j], 1, 3)) if ' '.join(x) in set(New_vocabulary_list)])
list_of_keywords.append(temp)
I've obtained keywords list as
['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
['sleep', 'anxiety', 'lack of sleep']
How can I simply the results by removing all substring within the list and remain:
['high blood pressure']
['anxiety', 'lack of sleep']
python nlp
add a comment |
I've extracted keywords based on 1-gram, 2-gram, 3-gram within a tokenized sentence
list_of_keywords = []
for i in range(0, len(stemmed_words)):
temp = []
for j in range(0, len(stemmed_words[i])):
temp.append([' '.join(x) for x in list(everygrams(stemmed_words[i][j], 1, 3)) if ' '.join(x) in set(New_vocabulary_list)])
list_of_keywords.append(temp)
I've obtained keywords list as
['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
['sleep', 'anxiety', 'lack of sleep']
How can I simply the results by removing all substring within the list and remain:
['high blood pressure']
['anxiety', 'lack of sleep']
python nlp
Will all sub strings be split by a space? What should['sub', 'string', 'substring']
become?
– Peilonrayz
yesterday
add a comment |
I've extracted keywords based on 1-gram, 2-gram, 3-gram within a tokenized sentence
list_of_keywords = []
for i in range(0, len(stemmed_words)):
temp = []
for j in range(0, len(stemmed_words[i])):
temp.append([' '.join(x) for x in list(everygrams(stemmed_words[i][j], 1, 3)) if ' '.join(x) in set(New_vocabulary_list)])
list_of_keywords.append(temp)
I've obtained keywords list as
['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
['sleep', 'anxiety', 'lack of sleep']
How can I simply the results by removing all substring within the list and remain:
['high blood pressure']
['anxiety', 'lack of sleep']
python nlp
I've extracted keywords based on 1-gram, 2-gram, 3-gram within a tokenized sentence
list_of_keywords = []
for i in range(0, len(stemmed_words)):
temp = []
for j in range(0, len(stemmed_words[i])):
temp.append([' '.join(x) for x in list(everygrams(stemmed_words[i][j], 1, 3)) if ' '.join(x) in set(New_vocabulary_list)])
list_of_keywords.append(temp)
I've obtained keywords list as
['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
['sleep', 'anxiety', 'lack of sleep']
How can I simply the results by removing all substring within the list and remain:
['high blood pressure']
['anxiety', 'lack of sleep']
python nlp
python nlp
asked yesterday
LisaLisa
818
818
Will all sub strings be split by a space? What should['sub', 'string', 'substring']
become?
– Peilonrayz
yesterday
add a comment |
Will all sub strings be split by a space? What should['sub', 'string', 'substring']
become?
– Peilonrayz
yesterday
Will all sub strings be split by a space? What should
['sub', 'string', 'substring']
become?– Peilonrayz
yesterday
Will all sub strings be split by a space? What should
['sub', 'string', 'substring']
become?– Peilonrayz
yesterday
add a comment |
5 Answers
5
active
oldest
votes
You could use this one liner:
b = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
result = [ i for i in b if not any( [ i in a for a in b if a != i] )]
I admit this is O(n2) and maybe will be slow in performance for large inputs.
This is basically a list comprehension of the following:
word_list = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
result = []
for this_word in word_list:
words_without_this_word = [ other_word for other_word in word_list if other_word != this_word]
found = False
for other_word in words_without_this_word:
if this_word in other_word:
found = True
if not found:
result.append(this_word)
result
1
I believe this will be slightly faster by removing the inner list comprehension, such that it becomes a generator comprehension, like so:result = [i for i in b if not any(i in a for a in b if a != i)]
– beruic
yesterday
add a comment |
If you have a large list of words, it might be a good idea to use a suffix tree.
Here's a package on PyPI.
Once you created the tree, you can call find_all(word)
to get the index of every occurence of word
. You simply need to keep the strings which only appear once:
from suffix_trees import STree
# https://pypi.org/project/suffix-trees/
# pip install suffix-trees
words = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure'] + ['sleep', 'anxiety', 'lack of sleep']
st = STree.STree(words)
st.find_all('blood')
# [0, 20, 26, 46]
st.find_all('high blood pressure')
# [41]
[word for word in words if len(st.find_all(word)) == 1]
# ['high blood pressure', 'anxiety', 'lack of sleep']
words
needs to be a unique list of strings, so you might need to call list(set(words))
before generating the suffix-tree.
As far as I can tell, the whole script should run in O(n)
, with n
being the total length of the strings.
add a comment |
assuming that order of your elements is from shortest string to longest string, you need to check if each element is substring of last one and then remove it from the list:
symptoms = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
def removeSubstring(data):
for symptom in data[:-1]:
if symptom in data[-1]:
print("Removing: ", symptom)
data.remove(symptom)
print(data)
removeSubstring(symptoms)
New contributor
Thanks, but the way u suggested would be only workable for 1 longest string, simply tried withsymptoms = ['blood', 'sleep', 'high blood pressure', 'lack of sleep']
– Lisa
yesterday
1
It’s normally a real bad idea to remove things from a list while you are iterating over it.
– Christian Sloper
yesterday
@ChristianSloper can you elaborate why?
– Anna Janiszewska
yesterday
quora.com/…
– Christian Sloper
yesterday
add a comment |
words = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
superset_word = ''
#print (words)
for word in words:
word_list_minus_word = [each for each in words if word != each]
counter = 0
for other_word in word_list_minus_word:
if (other_word not in word):
break
else:
counter += 1
if (counter == len(word_list_minus_word)):
superset_word = word
break
print(superset_word)
This does not work on OP's second example
– Christian Sloper
yesterday
add a comment |
grams = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
unique_grams = [grams[i] for i in range(len(grams)) if not grams[i] in ' '.join(grams[i+1:])]
New contributor
It doesn't seem to work. For example withgrams = ['a b c', 'b c', 'a', 'b', 'c']
.
– Eric Duminil
yesterday
add a comment |
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
);
);
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%2f55179517%2fpython-check-if-string-and-its-substring-are-existing-in-the-same-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could use this one liner:
b = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
result = [ i for i in b if not any( [ i in a for a in b if a != i] )]
I admit this is O(n2) and maybe will be slow in performance for large inputs.
This is basically a list comprehension of the following:
word_list = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
result = []
for this_word in word_list:
words_without_this_word = [ other_word for other_word in word_list if other_word != this_word]
found = False
for other_word in words_without_this_word:
if this_word in other_word:
found = True
if not found:
result.append(this_word)
result
1
I believe this will be slightly faster by removing the inner list comprehension, such that it becomes a generator comprehension, like so:result = [i for i in b if not any(i in a for a in b if a != i)]
– beruic
yesterday
add a comment |
You could use this one liner:
b = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
result = [ i for i in b if not any( [ i in a for a in b if a != i] )]
I admit this is O(n2) and maybe will be slow in performance for large inputs.
This is basically a list comprehension of the following:
word_list = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
result = []
for this_word in word_list:
words_without_this_word = [ other_word for other_word in word_list if other_word != this_word]
found = False
for other_word in words_without_this_word:
if this_word in other_word:
found = True
if not found:
result.append(this_word)
result
1
I believe this will be slightly faster by removing the inner list comprehension, such that it becomes a generator comprehension, like so:result = [i for i in b if not any(i in a for a in b if a != i)]
– beruic
yesterday
add a comment |
You could use this one liner:
b = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
result = [ i for i in b if not any( [ i in a for a in b if a != i] )]
I admit this is O(n2) and maybe will be slow in performance for large inputs.
This is basically a list comprehension of the following:
word_list = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
result = []
for this_word in word_list:
words_without_this_word = [ other_word for other_word in word_list if other_word != this_word]
found = False
for other_word in words_without_this_word:
if this_word in other_word:
found = True
if not found:
result.append(this_word)
result
You could use this one liner:
b = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
result = [ i for i in b if not any( [ i in a for a in b if a != i] )]
I admit this is O(n2) and maybe will be slow in performance for large inputs.
This is basically a list comprehension of the following:
word_list = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
result = []
for this_word in word_list:
words_without_this_word = [ other_word for other_word in word_list if other_word != this_word]
found = False
for other_word in words_without_this_word:
if this_word in other_word:
found = True
if not found:
result.append(this_word)
result
edited yesterday
Chetan Ameta
5,96822137
5,96822137
answered yesterday
Christian SloperChristian Sloper
1,730416
1,730416
1
I believe this will be slightly faster by removing the inner list comprehension, such that it becomes a generator comprehension, like so:result = [i for i in b if not any(i in a for a in b if a != i)]
– beruic
yesterday
add a comment |
1
I believe this will be slightly faster by removing the inner list comprehension, such that it becomes a generator comprehension, like so:result = [i for i in b if not any(i in a for a in b if a != i)]
– beruic
yesterday
1
1
I believe this will be slightly faster by removing the inner list comprehension, such that it becomes a generator comprehension, like so:
result = [i for i in b if not any(i in a for a in b if a != i)]
– beruic
yesterday
I believe this will be slightly faster by removing the inner list comprehension, such that it becomes a generator comprehension, like so:
result = [i for i in b if not any(i in a for a in b if a != i)]
– beruic
yesterday
add a comment |
If you have a large list of words, it might be a good idea to use a suffix tree.
Here's a package on PyPI.
Once you created the tree, you can call find_all(word)
to get the index of every occurence of word
. You simply need to keep the strings which only appear once:
from suffix_trees import STree
# https://pypi.org/project/suffix-trees/
# pip install suffix-trees
words = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure'] + ['sleep', 'anxiety', 'lack of sleep']
st = STree.STree(words)
st.find_all('blood')
# [0, 20, 26, 46]
st.find_all('high blood pressure')
# [41]
[word for word in words if len(st.find_all(word)) == 1]
# ['high blood pressure', 'anxiety', 'lack of sleep']
words
needs to be a unique list of strings, so you might need to call list(set(words))
before generating the suffix-tree.
As far as I can tell, the whole script should run in O(n)
, with n
being the total length of the strings.
add a comment |
If you have a large list of words, it might be a good idea to use a suffix tree.
Here's a package on PyPI.
Once you created the tree, you can call find_all(word)
to get the index of every occurence of word
. You simply need to keep the strings which only appear once:
from suffix_trees import STree
# https://pypi.org/project/suffix-trees/
# pip install suffix-trees
words = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure'] + ['sleep', 'anxiety', 'lack of sleep']
st = STree.STree(words)
st.find_all('blood')
# [0, 20, 26, 46]
st.find_all('high blood pressure')
# [41]
[word for word in words if len(st.find_all(word)) == 1]
# ['high blood pressure', 'anxiety', 'lack of sleep']
words
needs to be a unique list of strings, so you might need to call list(set(words))
before generating the suffix-tree.
As far as I can tell, the whole script should run in O(n)
, with n
being the total length of the strings.
add a comment |
If you have a large list of words, it might be a good idea to use a suffix tree.
Here's a package on PyPI.
Once you created the tree, you can call find_all(word)
to get the index of every occurence of word
. You simply need to keep the strings which only appear once:
from suffix_trees import STree
# https://pypi.org/project/suffix-trees/
# pip install suffix-trees
words = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure'] + ['sleep', 'anxiety', 'lack of sleep']
st = STree.STree(words)
st.find_all('blood')
# [0, 20, 26, 46]
st.find_all('high blood pressure')
# [41]
[word for word in words if len(st.find_all(word)) == 1]
# ['high blood pressure', 'anxiety', 'lack of sleep']
words
needs to be a unique list of strings, so you might need to call list(set(words))
before generating the suffix-tree.
As far as I can tell, the whole script should run in O(n)
, with n
being the total length of the strings.
If you have a large list of words, it might be a good idea to use a suffix tree.
Here's a package on PyPI.
Once you created the tree, you can call find_all(word)
to get the index of every occurence of word
. You simply need to keep the strings which only appear once:
from suffix_trees import STree
# https://pypi.org/project/suffix-trees/
# pip install suffix-trees
words = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure'] + ['sleep', 'anxiety', 'lack of sleep']
st = STree.STree(words)
st.find_all('blood')
# [0, 20, 26, 46]
st.find_all('high blood pressure')
# [41]
[word for word in words if len(st.find_all(word)) == 1]
# ['high blood pressure', 'anxiety', 'lack of sleep']
words
needs to be a unique list of strings, so you might need to call list(set(words))
before generating the suffix-tree.
As far as I can tell, the whole script should run in O(n)
, with n
being the total length of the strings.
edited yesterday
answered yesterday
Eric DuminilEric Duminil
40.5k63271
40.5k63271
add a comment |
add a comment |
assuming that order of your elements is from shortest string to longest string, you need to check if each element is substring of last one and then remove it from the list:
symptoms = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
def removeSubstring(data):
for symptom in data[:-1]:
if symptom in data[-1]:
print("Removing: ", symptom)
data.remove(symptom)
print(data)
removeSubstring(symptoms)
New contributor
Thanks, but the way u suggested would be only workable for 1 longest string, simply tried withsymptoms = ['blood', 'sleep', 'high blood pressure', 'lack of sleep']
– Lisa
yesterday
1
It’s normally a real bad idea to remove things from a list while you are iterating over it.
– Christian Sloper
yesterday
@ChristianSloper can you elaborate why?
– Anna Janiszewska
yesterday
quora.com/…
– Christian Sloper
yesterday
add a comment |
assuming that order of your elements is from shortest string to longest string, you need to check if each element is substring of last one and then remove it from the list:
symptoms = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
def removeSubstring(data):
for symptom in data[:-1]:
if symptom in data[-1]:
print("Removing: ", symptom)
data.remove(symptom)
print(data)
removeSubstring(symptoms)
New contributor
Thanks, but the way u suggested would be only workable for 1 longest string, simply tried withsymptoms = ['blood', 'sleep', 'high blood pressure', 'lack of sleep']
– Lisa
yesterday
1
It’s normally a real bad idea to remove things from a list while you are iterating over it.
– Christian Sloper
yesterday
@ChristianSloper can you elaborate why?
– Anna Janiszewska
yesterday
quora.com/…
– Christian Sloper
yesterday
add a comment |
assuming that order of your elements is from shortest string to longest string, you need to check if each element is substring of last one and then remove it from the list:
symptoms = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
def removeSubstring(data):
for symptom in data[:-1]:
if symptom in data[-1]:
print("Removing: ", symptom)
data.remove(symptom)
print(data)
removeSubstring(symptoms)
New contributor
assuming that order of your elements is from shortest string to longest string, you need to check if each element is substring of last one and then remove it from the list:
symptoms = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
def removeSubstring(data):
for symptom in data[:-1]:
if symptom in data[-1]:
print("Removing: ", symptom)
data.remove(symptom)
print(data)
removeSubstring(symptoms)
New contributor
New contributor
answered yesterday
Anna JaniszewskaAnna Janiszewska
1
1
New contributor
New contributor
Thanks, but the way u suggested would be only workable for 1 longest string, simply tried withsymptoms = ['blood', 'sleep', 'high blood pressure', 'lack of sleep']
– Lisa
yesterday
1
It’s normally a real bad idea to remove things from a list while you are iterating over it.
– Christian Sloper
yesterday
@ChristianSloper can you elaborate why?
– Anna Janiszewska
yesterday
quora.com/…
– Christian Sloper
yesterday
add a comment |
Thanks, but the way u suggested would be only workable for 1 longest string, simply tried withsymptoms = ['blood', 'sleep', 'high blood pressure', 'lack of sleep']
– Lisa
yesterday
1
It’s normally a real bad idea to remove things from a list while you are iterating over it.
– Christian Sloper
yesterday
@ChristianSloper can you elaborate why?
– Anna Janiszewska
yesterday
quora.com/…
– Christian Sloper
yesterday
Thanks, but the way u suggested would be only workable for 1 longest string, simply tried with
symptoms = ['blood', 'sleep', 'high blood pressure', 'lack of sleep']
– Lisa
yesterday
Thanks, but the way u suggested would be only workable for 1 longest string, simply tried with
symptoms = ['blood', 'sleep', 'high blood pressure', 'lack of sleep']
– Lisa
yesterday
1
1
It’s normally a real bad idea to remove things from a list while you are iterating over it.
– Christian Sloper
yesterday
It’s normally a real bad idea to remove things from a list while you are iterating over it.
– Christian Sloper
yesterday
@ChristianSloper can you elaborate why?
– Anna Janiszewska
yesterday
@ChristianSloper can you elaborate why?
– Anna Janiszewska
yesterday
quora.com/…
– Christian Sloper
yesterday
quora.com/…
– Christian Sloper
yesterday
add a comment |
words = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
superset_word = ''
#print (words)
for word in words:
word_list_minus_word = [each for each in words if word != each]
counter = 0
for other_word in word_list_minus_word:
if (other_word not in word):
break
else:
counter += 1
if (counter == len(word_list_minus_word)):
superset_word = word
break
print(superset_word)
This does not work on OP's second example
– Christian Sloper
yesterday
add a comment |
words = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
superset_word = ''
#print (words)
for word in words:
word_list_minus_word = [each for each in words if word != each]
counter = 0
for other_word in word_list_minus_word:
if (other_word not in word):
break
else:
counter += 1
if (counter == len(word_list_minus_word)):
superset_word = word
break
print(superset_word)
This does not work on OP's second example
– Christian Sloper
yesterday
add a comment |
words = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
superset_word = ''
#print (words)
for word in words:
word_list_minus_word = [each for each in words if word != each]
counter = 0
for other_word in word_list_minus_word:
if (other_word not in word):
break
else:
counter += 1
if (counter == len(word_list_minus_word)):
superset_word = word
break
print(superset_word)
words = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
superset_word = ''
#print (words)
for word in words:
word_list_minus_word = [each for each in words if word != each]
counter = 0
for other_word in word_list_minus_word:
if (other_word not in word):
break
else:
counter += 1
if (counter == len(word_list_minus_word)):
superset_word = word
break
print(superset_word)
answered yesterday
Venfah NazirVenfah Nazir
8914
8914
This does not work on OP's second example
– Christian Sloper
yesterday
add a comment |
This does not work on OP's second example
– Christian Sloper
yesterday
This does not work on OP's second example
– Christian Sloper
yesterday
This does not work on OP's second example
– Christian Sloper
yesterday
add a comment |
grams = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
unique_grams = [grams[i] for i in range(len(grams)) if not grams[i] in ' '.join(grams[i+1:])]
New contributor
It doesn't seem to work. For example withgrams = ['a b c', 'b c', 'a', 'b', 'c']
.
– Eric Duminil
yesterday
add a comment |
grams = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
unique_grams = [grams[i] for i in range(len(grams)) if not grams[i] in ' '.join(grams[i+1:])]
New contributor
It doesn't seem to work. For example withgrams = ['a b c', 'b c', 'a', 'b', 'c']
.
– Eric Duminil
yesterday
add a comment |
grams = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
unique_grams = [grams[i] for i in range(len(grams)) if not grams[i] in ' '.join(grams[i+1:])]
New contributor
grams = ['blood', 'pressure', 'high blood', 'blood pressure', 'high blood pressure']
unique_grams = [grams[i] for i in range(len(grams)) if not grams[i] in ' '.join(grams[i+1:])]
New contributor
edited yesterday
Vasilis G.
3,7332824
3,7332824
New contributor
answered yesterday
Jawad Ali KhanJawad Ali Khan
1
1
New contributor
New contributor
It doesn't seem to work. For example withgrams = ['a b c', 'b c', 'a', 'b', 'c']
.
– Eric Duminil
yesterday
add a comment |
It doesn't seem to work. For example withgrams = ['a b c', 'b c', 'a', 'b', 'c']
.
– Eric Duminil
yesterday
It doesn't seem to work. For example with
grams = ['a b c', 'b c', 'a', 'b', 'c']
.– Eric Duminil
yesterday
It doesn't seem to work. For example with
grams = ['a b c', 'b c', 'a', 'b', 'c']
.– Eric Duminil
yesterday
add a 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.
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%2f55179517%2fpython-check-if-string-and-its-substring-are-existing-in-the-same-list%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
Will all sub strings be split by a space? What should
['sub', 'string', 'substring']
become?– Peilonrayz
yesterday