How to check if a certain string repeats more than once in a list












0















In my code, I want to check whether I could check if a certain string e.g



a="."


repeats more than once in a list. e.g



b=[".", ".", "Hello world"]


how would I do that?










share|improve this question


















  • 1





    b.count(a) > 1

    – sashaaero
    Nov 22 '18 at 7:04
















0















In my code, I want to check whether I could check if a certain string e.g



a="."


repeats more than once in a list. e.g



b=[".", ".", "Hello world"]


how would I do that?










share|improve this question


















  • 1





    b.count(a) > 1

    – sashaaero
    Nov 22 '18 at 7:04














0












0








0








In my code, I want to check whether I could check if a certain string e.g



a="."


repeats more than once in a list. e.g



b=[".", ".", "Hello world"]


how would I do that?










share|improve this question














In my code, I want to check whether I could check if a certain string e.g



a="."


repeats more than once in a list. e.g



b=[".", ".", "Hello world"]


how would I do that?







python python-3.x






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 7:03









Coder CodyCoder Cody

408




408








  • 1





    b.count(a) > 1

    – sashaaero
    Nov 22 '18 at 7:04














  • 1





    b.count(a) > 1

    – sashaaero
    Nov 22 '18 at 7:04








1




1





b.count(a) > 1

– sashaaero
Nov 22 '18 at 7:04





b.count(a) > 1

– sashaaero
Nov 22 '18 at 7:04












5 Answers
5






active

oldest

votes


















3














b=[".", ".", "Hello world"]
print(b.count(a))
>>> 2


Use count function.






share|improve this answer































    2














    You can use the built in count method



    n_occurences = b.count(a)





    share|improve this answer































      1














      Use collections.Counter() or simply list.count()



      list.count(x) will return the count of x in list



      b=[".", ".", "Hello world"]
      # Check for "."
      print(b.count(".")


      collections.Counter will return a dictionary, which have count information of every item in a list:



      from collections import Counter

      b=[".", ".", "Hello world"]
      # Check for "."
      count = b.Counter()

      if count["."] > 1:
      print("More than one occurance")





      share|improve this answer


























      • I think collections.Counter is overkill for this case.

        – sashaaero
        Nov 22 '18 at 7:05











      • @sashaaero you are right. I've read OP carelessly and only come with the idea of using Counter. I've made an edit, count() is sufficient for such tasks :D

        – enamoria
        Nov 22 '18 at 7:09



















      1














      Use count function.



          a="."
      b=[".", ".", "Hello world"]
      if b.count(a) > 1:
      print("more than 1 time")





      share|improve this answer































        1














        Using collections.Counter:



        from collections import Counter

        a = "."
        b=[".", ".", "Hello world"]

        print(Counter(b)[a]) # 2





        share|improve this answer























          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%2f53425474%2fhow-to-check-if-a-certain-string-repeats-more-than-once-in-a-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









          3














          b=[".", ".", "Hello world"]
          print(b.count(a))
          >>> 2


          Use count function.






          share|improve this answer




























            3














            b=[".", ".", "Hello world"]
            print(b.count(a))
            >>> 2


            Use count function.






            share|improve this answer


























              3












              3








              3







              b=[".", ".", "Hello world"]
              print(b.count(a))
              >>> 2


              Use count function.






              share|improve this answer













              b=[".", ".", "Hello world"]
              print(b.count(a))
              >>> 2


              Use count function.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 22 '18 at 7:04









              Sandesh34Sandesh34

              254113




              254113

























                  2














                  You can use the built in count method



                  n_occurences = b.count(a)





                  share|improve this answer




























                    2














                    You can use the built in count method



                    n_occurences = b.count(a)





                    share|improve this answer


























                      2












                      2








                      2







                      You can use the built in count method



                      n_occurences = b.count(a)





                      share|improve this answer













                      You can use the built in count method



                      n_occurences = b.count(a)






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 22 '18 at 7:04









                      Sven HarrisSven Harris

                      2,1881515




                      2,1881515























                          1














                          Use collections.Counter() or simply list.count()



                          list.count(x) will return the count of x in list



                          b=[".", ".", "Hello world"]
                          # Check for "."
                          print(b.count(".")


                          collections.Counter will return a dictionary, which have count information of every item in a list:



                          from collections import Counter

                          b=[".", ".", "Hello world"]
                          # Check for "."
                          count = b.Counter()

                          if count["."] > 1:
                          print("More than one occurance")





                          share|improve this answer


























                          • I think collections.Counter is overkill for this case.

                            – sashaaero
                            Nov 22 '18 at 7:05











                          • @sashaaero you are right. I've read OP carelessly and only come with the idea of using Counter. I've made an edit, count() is sufficient for such tasks :D

                            – enamoria
                            Nov 22 '18 at 7:09
















                          1














                          Use collections.Counter() or simply list.count()



                          list.count(x) will return the count of x in list



                          b=[".", ".", "Hello world"]
                          # Check for "."
                          print(b.count(".")


                          collections.Counter will return a dictionary, which have count information of every item in a list:



                          from collections import Counter

                          b=[".", ".", "Hello world"]
                          # Check for "."
                          count = b.Counter()

                          if count["."] > 1:
                          print("More than one occurance")





                          share|improve this answer


























                          • I think collections.Counter is overkill for this case.

                            – sashaaero
                            Nov 22 '18 at 7:05











                          • @sashaaero you are right. I've read OP carelessly and only come with the idea of using Counter. I've made an edit, count() is sufficient for such tasks :D

                            – enamoria
                            Nov 22 '18 at 7:09














                          1












                          1








                          1







                          Use collections.Counter() or simply list.count()



                          list.count(x) will return the count of x in list



                          b=[".", ".", "Hello world"]
                          # Check for "."
                          print(b.count(".")


                          collections.Counter will return a dictionary, which have count information of every item in a list:



                          from collections import Counter

                          b=[".", ".", "Hello world"]
                          # Check for "."
                          count = b.Counter()

                          if count["."] > 1:
                          print("More than one occurance")





                          share|improve this answer















                          Use collections.Counter() or simply list.count()



                          list.count(x) will return the count of x in list



                          b=[".", ".", "Hello world"]
                          # Check for "."
                          print(b.count(".")


                          collections.Counter will return a dictionary, which have count information of every item in a list:



                          from collections import Counter

                          b=[".", ".", "Hello world"]
                          # Check for "."
                          count = b.Counter()

                          if count["."] > 1:
                          print("More than one occurance")






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 22 '18 at 7:07

























                          answered Nov 22 '18 at 7:05









                          enamoriaenamoria

                          617621




                          617621













                          • I think collections.Counter is overkill for this case.

                            – sashaaero
                            Nov 22 '18 at 7:05











                          • @sashaaero you are right. I've read OP carelessly and only come with the idea of using Counter. I've made an edit, count() is sufficient for such tasks :D

                            – enamoria
                            Nov 22 '18 at 7:09



















                          • I think collections.Counter is overkill for this case.

                            – sashaaero
                            Nov 22 '18 at 7:05











                          • @sashaaero you are right. I've read OP carelessly and only come with the idea of using Counter. I've made an edit, count() is sufficient for such tasks :D

                            – enamoria
                            Nov 22 '18 at 7:09

















                          I think collections.Counter is overkill for this case.

                          – sashaaero
                          Nov 22 '18 at 7:05





                          I think collections.Counter is overkill for this case.

                          – sashaaero
                          Nov 22 '18 at 7:05













                          @sashaaero you are right. I've read OP carelessly and only come with the idea of using Counter. I've made an edit, count() is sufficient for such tasks :D

                          – enamoria
                          Nov 22 '18 at 7:09





                          @sashaaero you are right. I've read OP carelessly and only come with the idea of using Counter. I've made an edit, count() is sufficient for such tasks :D

                          – enamoria
                          Nov 22 '18 at 7:09











                          1














                          Use count function.



                              a="."
                          b=[".", ".", "Hello world"]
                          if b.count(a) > 1:
                          print("more than 1 time")





                          share|improve this answer




























                            1














                            Use count function.



                                a="."
                            b=[".", ".", "Hello world"]
                            if b.count(a) > 1:
                            print("more than 1 time")





                            share|improve this answer


























                              1












                              1








                              1







                              Use count function.



                                  a="."
                              b=[".", ".", "Hello world"]
                              if b.count(a) > 1:
                              print("more than 1 time")





                              share|improve this answer













                              Use count function.



                                  a="."
                              b=[".", ".", "Hello world"]
                              if b.count(a) > 1:
                              print("more than 1 time")






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 22 '18 at 7:09









                              UsmanUsman

                              800614




                              800614























                                  1














                                  Using collections.Counter:



                                  from collections import Counter

                                  a = "."
                                  b=[".", ".", "Hello world"]

                                  print(Counter(b)[a]) # 2





                                  share|improve this answer




























                                    1














                                    Using collections.Counter:



                                    from collections import Counter

                                    a = "."
                                    b=[".", ".", "Hello world"]

                                    print(Counter(b)[a]) # 2





                                    share|improve this answer


























                                      1












                                      1








                                      1







                                      Using collections.Counter:



                                      from collections import Counter

                                      a = "."
                                      b=[".", ".", "Hello world"]

                                      print(Counter(b)[a]) # 2





                                      share|improve this answer













                                      Using collections.Counter:



                                      from collections import Counter

                                      a = "."
                                      b=[".", ".", "Hello world"]

                                      print(Counter(b)[a]) # 2






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 22 '18 at 7:17









                                      b-fgb-fg

                                      1,95911522




                                      1,95911522






























                                          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%2f53425474%2fhow-to-check-if-a-certain-string-repeats-more-than-once-in-a-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

                                          MongoDB - Not Authorized To Execute Command

                                          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                                          Npm cannot find a required file even through it is in the searched directory