Showing words in file that start with a specific letter [duplicate]












0
















This question already has an answer here:




  • Iterator Object for Removing Duplicates in Python

    2 answers




My code is able to show each word that starts with a specific letter from a text file, but I want it to not show duplicate words. Here is my code:



with open('text.txt','r') as myFile:
data=myFile.read().lower()

for s in data.split():
if s.startswith("r"):
print(s)


Like I said, my code does print the words but it shows duplicates. Thank you for helping










share|improve this question













marked as duplicate by MisterMiyagi, jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 23:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    0
















    This question already has an answer here:




    • Iterator Object for Removing Duplicates in Python

      2 answers




    My code is able to show each word that starts with a specific letter from a text file, but I want it to not show duplicate words. Here is my code:



    with open('text.txt','r') as myFile:
    data=myFile.read().lower()

    for s in data.split():
    if s.startswith("r"):
    print(s)


    Like I said, my code does print the words but it shows duplicates. Thank you for helping










    share|improve this question













    marked as duplicate by MisterMiyagi, jpp python
    Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 13 '18 at 23:53


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      0












      0








      0









      This question already has an answer here:




      • Iterator Object for Removing Duplicates in Python

        2 answers




      My code is able to show each word that starts with a specific letter from a text file, but I want it to not show duplicate words. Here is my code:



      with open('text.txt','r') as myFile:
      data=myFile.read().lower()

      for s in data.split():
      if s.startswith("r"):
      print(s)


      Like I said, my code does print the words but it shows duplicates. Thank you for helping










      share|improve this question















      This question already has an answer here:




      • Iterator Object for Removing Duplicates in Python

        2 answers




      My code is able to show each word that starts with a specific letter from a text file, but I want it to not show duplicate words. Here is my code:



      with open('text.txt','r') as myFile:
      data=myFile.read().lower()

      for s in data.split():
      if s.startswith("r"):
      print(s)


      Like I said, my code does print the words but it shows duplicates. Thank you for helping





      This question already has an answer here:




      • Iterator Object for Removing Duplicates in Python

        2 answers








      python python-3.x file-io






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 20:08









      gbenyugbenyu

      224




      224




      marked as duplicate by MisterMiyagi, jpp python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 13 '18 at 23:53


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by MisterMiyagi, jpp python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 13 '18 at 23:53


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          2 Answers
          2






          active

          oldest

          votes


















          2














          Use a set:



          with open('text.txt', 'r') as myFile:
          data = myFile.read().lower()

          seen = set()
          for s in data.split():
          if s not in seen and s.startswith("r"):
          seen.add(s)
          print(s)





          share|improve this answer































            0














            This is an optimized version the will read the file line by line without loading it all into memory:



            seen_words = set()

            with open('text.txt', 'r') as my_file:
            for line in my_file:
            for word in line.lower().split():
            if word not in seen_words:
            print(word)
            seen_words.add(word)





            share|improve this answer






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              Use a set:



              with open('text.txt', 'r') as myFile:
              data = myFile.read().lower()

              seen = set()
              for s in data.split():
              if s not in seen and s.startswith("r"):
              seen.add(s)
              print(s)





              share|improve this answer




























                2














                Use a set:



                with open('text.txt', 'r') as myFile:
                data = myFile.read().lower()

                seen = set()
                for s in data.split():
                if s not in seen and s.startswith("r"):
                seen.add(s)
                print(s)





                share|improve this answer


























                  2












                  2








                  2







                  Use a set:



                  with open('text.txt', 'r') as myFile:
                  data = myFile.read().lower()

                  seen = set()
                  for s in data.split():
                  if s not in seen and s.startswith("r"):
                  seen.add(s)
                  print(s)





                  share|improve this answer













                  Use a set:



                  with open('text.txt', 'r') as myFile:
                  data = myFile.read().lower()

                  seen = set()
                  for s in data.split():
                  if s not in seen and s.startswith("r"):
                  seen.add(s)
                  print(s)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 20:09









                  Daniel MesejoDaniel Mesejo

                  16.3k21330




                  16.3k21330

























                      0














                      This is an optimized version the will read the file line by line without loading it all into memory:



                      seen_words = set()

                      with open('text.txt', 'r') as my_file:
                      for line in my_file:
                      for word in line.lower().split():
                      if word not in seen_words:
                      print(word)
                      seen_words.add(word)





                      share|improve this answer




























                        0














                        This is an optimized version the will read the file line by line without loading it all into memory:



                        seen_words = set()

                        with open('text.txt', 'r') as my_file:
                        for line in my_file:
                        for word in line.lower().split():
                        if word not in seen_words:
                        print(word)
                        seen_words.add(word)





                        share|improve this answer


























                          0












                          0








                          0







                          This is an optimized version the will read the file line by line without loading it all into memory:



                          seen_words = set()

                          with open('text.txt', 'r') as my_file:
                          for line in my_file:
                          for word in line.lower().split():
                          if word not in seen_words:
                          print(word)
                          seen_words.add(word)





                          share|improve this answer













                          This is an optimized version the will read the file line by line without loading it all into memory:



                          seen_words = set()

                          with open('text.txt', 'r') as my_file:
                          for line in my_file:
                          for word in line.lower().split():
                          if word not in seen_words:
                          print(word)
                          seen_words.add(word)






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 13 '18 at 20:13









                          DalvenjiaDalvenjia

                          968612




                          968612















                              Popular posts from this blog

                              Xamarin.iOS Cant Deploy on Iphone

                              Glorious Revolution

                              Dulmage-Mendelsohn matrix decomposition in Python