Python: Check if string and its substring are existing in the same list

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








12















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
    Mar 15 at 17:30


















12















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
    Mar 15 at 17:30














12












12








12


1






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 Mar 15 at 9:36









LisaLisa

918




918












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

    – Peilonrayz
    Mar 15 at 17:30


















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

    – Peilonrayz
    Mar 15 at 17:30

















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

– Peilonrayz
Mar 15 at 17:30






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

– Peilonrayz
Mar 15 at 17:30













5 Answers
5






active

oldest

votes


















13














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
    Mar 15 at 12:16


















1














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























    • 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
      Mar 15 at 10:15






    • 2





      It’s normally a real bad idea to remove things from a list while you are iterating over it.

      – Christian Sloper
      Mar 15 at 10:26











    • @ChristianSloper can you elaborate why?

      – Anna Janiszewska
      Mar 15 at 10:32











    • quora.com/…

      – Christian Sloper
      Mar 15 at 10:36


















    -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
      Mar 15 at 10:45


















    -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

























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

      – Eric Duminil
      Mar 15 at 17:07











    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









    13














    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
      Mar 15 at 12:16















    13














    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
      Mar 15 at 12:16













    13












    13








    13







    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 Mar 15 at 9:55









    Chetan Ameta

    6,03822237




    6,03822237










    answered Mar 15 at 9:42









    Christian SloperChristian Sloper

    2,776418




    2,776418







    • 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
      Mar 15 at 12:16












    • 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
      Mar 15 at 12:16







    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
    Mar 15 at 12:16





    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
    Mar 15 at 12:16













    1














    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














      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












        1








        1







        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 Mar 15 at 17:14

























        answered Mar 15 at 17:03









        Eric DuminilEric Duminil

        41k63472




        41k63472





















            -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























            • 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
              Mar 15 at 10:15






            • 2





              It’s normally a real bad idea to remove things from a list while you are iterating over it.

              – Christian Sloper
              Mar 15 at 10:26











            • @ChristianSloper can you elaborate why?

              – Anna Janiszewska
              Mar 15 at 10:32











            • quora.com/…

              – Christian Sloper
              Mar 15 at 10:36















            -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























            • 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
              Mar 15 at 10:15






            • 2





              It’s normally a real bad idea to remove things from a list while you are iterating over it.

              – Christian Sloper
              Mar 15 at 10:26











            • @ChristianSloper can you elaborate why?

              – Anna Janiszewska
              Mar 15 at 10:32











            • quora.com/…

              – Christian Sloper
              Mar 15 at 10:36













            -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













            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












            share|improve this answer



            share|improve this answer










            answered Mar 15 at 10:07









            Anna JaniszewskaAnna Janiszewska

            1




            1












            • 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
              Mar 15 at 10:15






            • 2





              It’s normally a real bad idea to remove things from a list while you are iterating over it.

              – Christian Sloper
              Mar 15 at 10:26











            • @ChristianSloper can you elaborate why?

              – Anna Janiszewska
              Mar 15 at 10:32











            • quora.com/…

              – Christian Sloper
              Mar 15 at 10:36

















            • 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
              Mar 15 at 10:15






            • 2





              It’s normally a real bad idea to remove things from a list while you are iterating over it.

              – Christian Sloper
              Mar 15 at 10:26











            • @ChristianSloper can you elaborate why?

              – Anna Janiszewska
              Mar 15 at 10:32











            • quora.com/…

              – Christian Sloper
              Mar 15 at 10:36
















            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
            Mar 15 at 10:15





            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
            Mar 15 at 10:15




            2




            2





            It’s normally a real bad idea to remove things from a list while you are iterating over it.

            – Christian Sloper
            Mar 15 at 10:26





            It’s normally a real bad idea to remove things from a list while you are iterating over it.

            – Christian Sloper
            Mar 15 at 10:26













            @ChristianSloper can you elaborate why?

            – Anna Janiszewska
            Mar 15 at 10:32





            @ChristianSloper can you elaborate why?

            – Anna Janiszewska
            Mar 15 at 10:32













            quora.com/…

            – Christian Sloper
            Mar 15 at 10:36





            quora.com/…

            – Christian Sloper
            Mar 15 at 10:36











            -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
              Mar 15 at 10:45















            -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
              Mar 15 at 10:45













            -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 Mar 15 at 10:09









            Venfah NazirVenfah Nazir

            10914




            10914












            • This does not work on OP's second example

              – Christian Sloper
              Mar 15 at 10:45

















            • This does not work on OP's second example

              – Christian Sloper
              Mar 15 at 10:45
















            This does not work on OP's second example

            – Christian Sloper
            Mar 15 at 10:45





            This does not work on OP's second example

            – Christian Sloper
            Mar 15 at 10:45











            -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

























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

              – Eric Duminil
              Mar 15 at 17:07















            -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

























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

              – Eric Duminil
              Mar 15 at 17:07













            -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















            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














            share|improve this answer



            share|improve this answer








            edited Mar 15 at 18:40









            Vasilis G.

            4,0452924




            4,0452924










            answered Mar 15 at 12:21









            Jawad Ali KhanJawad Ali Khan

            1




            1












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

              – Eric Duminil
              Mar 15 at 17:07

















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

              – Eric Duminil
              Mar 15 at 17:07
















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

            – Eric Duminil
            Mar 15 at 17:07





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

            – Eric Duminil
            Mar 15 at 17:07

















            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

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay