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?










11















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









share|improve this question






















  • Will all sub strings be split by a space? What should ['sub', 'string', 'substring'] become?

    – Peilonrayz
    yesterday
















11















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









share|improve this question






















  • Will all sub strings be split by a space? What should ['sub', 'string', 'substring'] become?

    – Peilonrayz
    yesterday














11












11








11








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









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









LisaLisa

818




818












  • 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






Will all sub strings be split by a space? What should ['sub', 'string', 'substring'] become?

– Peilonrayz
yesterday













5 Answers
5






active

oldest

votes


















12














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





share|improve this answer




















  • 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


















0














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.






share|improve this answer
































    -1














    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)





    share|improve this answer








    New contributor




    Anna Janiszewska is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.




















    • 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





      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


















    -1














    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)





    share|improve this answer























    • This does not work on OP's second example

      – Christian Sloper
      yesterday


















    -3














    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:])]





    share|improve this answer










    New contributor




    Jawad Ali Khan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.




















    • It doesn't seem to work. For example with grams = ['a b c', 'b c', 'a', 'b', 'c'].

      – Eric Duminil
      yesterday










    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%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









    12














    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





    share|improve this answer




















    • 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















    12














    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





    share|improve this answer




















    • 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













    12












    12








    12







    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





    share|improve this answer















    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






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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












    • 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













    0














    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.






    share|improve this answer





























      0














      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.






      share|improve this answer



























        0












        0








        0







        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.






        share|improve this answer















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited yesterday

























        answered yesterday









        Eric DuminilEric Duminil

        40.5k63271




        40.5k63271





















            -1














            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)





            share|improve this answer








            New contributor




            Anna Janiszewska is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.




















            • 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





              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















            -1














            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)





            share|improve this answer








            New contributor




            Anna Janiszewska is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.




















            • 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





              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













            -1












            -1








            -1







            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)





            share|improve this answer








            New contributor




            Anna Janiszewska is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.










            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)






            share|improve this answer








            New contributor




            Anna Janiszewska is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer






            New contributor




            Anna Janiszewska is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered yesterday









            Anna JaniszewskaAnna Janiszewska

            1




            1




            New contributor




            Anna Janiszewska is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            Anna Janiszewska is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            Anna Janiszewska is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.












            • 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





              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






            • 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











            -1














            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)





            share|improve this answer























            • This does not work on OP's second example

              – Christian Sloper
              yesterday















            -1














            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)





            share|improve this answer























            • This does not work on OP's second example

              – Christian Sloper
              yesterday













            -1












            -1








            -1







            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)





            share|improve this answer













            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)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered yesterday









            Venfah NazirVenfah Nazir

            8914




            8914












            • 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





            This does not work on OP's second example

            – Christian Sloper
            yesterday











            -3














            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:])]





            share|improve this answer










            New contributor




            Jawad Ali Khan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.




















            • It doesn't seem to work. For example with grams = ['a b c', 'b c', 'a', 'b', 'c'].

              – Eric Duminil
              yesterday















            -3














            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:])]





            share|improve this answer










            New contributor




            Jawad Ali Khan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.




















            • It doesn't seem to work. For example with grams = ['a b c', 'b c', 'a', 'b', 'c'].

              – Eric Duminil
              yesterday













            -3












            -3








            -3







            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:])]





            share|improve this answer










            New contributor




            Jawad Ali Khan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.










            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:])]






            share|improve this answer










            New contributor




            Jawad Ali Khan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer








            edited yesterday









            Vasilis G.

            3,7332824




            3,7332824






            New contributor




            Jawad Ali Khan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered yesterday









            Jawad Ali KhanJawad Ali Khan

            1




            1




            New contributor




            Jawad Ali Khan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            Jawad Ali Khan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            Jawad Ali Khan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.












            • 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
















            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

















            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%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





















































            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

            Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

            Luettelo Yhdysvaltain laivaston lentotukialuksista Lähteet | Navigointivalikko

            Gary (muusikko) Sisällysluettelo Historia | Rockin' High | Lähteet | Aiheesta muualla | NavigointivalikkoInfobox OKTuomas "Gary" Keskinen Ancaran kitaristiksiProjekti Rockin' High