Check if a list of strings contains letters from another list












0















So I have two lists:



vocabulary = ['a','b','c']
sentences = ['a a b b c c', 'a c b c', 'b c c a b']


I want to calculate how many times the letters in vocabulary appear in the strings in the list sentences.



So I want the output to be:



a = 4
b = 5
c = 6


My program:



counter = Counter()
for word in sentences:
if word in vocabulary:
counter.update(word)
print(counter)


But I keep getting the output:



Counter()









share|improve this question





























    0















    So I have two lists:



    vocabulary = ['a','b','c']
    sentences = ['a a b b c c', 'a c b c', 'b c c a b']


    I want to calculate how many times the letters in vocabulary appear in the strings in the list sentences.



    So I want the output to be:



    a = 4
    b = 5
    c = 6


    My program:



    counter = Counter()
    for word in sentences:
    if word in vocabulary:
    counter.update(word)
    print(counter)


    But I keep getting the output:



    Counter()









    share|improve this question



























      0












      0








      0








      So I have two lists:



      vocabulary = ['a','b','c']
      sentences = ['a a b b c c', 'a c b c', 'b c c a b']


      I want to calculate how many times the letters in vocabulary appear in the strings in the list sentences.



      So I want the output to be:



      a = 4
      b = 5
      c = 6


      My program:



      counter = Counter()
      for word in sentences:
      if word in vocabulary:
      counter.update(word)
      print(counter)


      But I keep getting the output:



      Counter()









      share|improve this question
















      So I have two lists:



      vocabulary = ['a','b','c']
      sentences = ['a a b b c c', 'a c b c', 'b c c a b']


      I want to calculate how many times the letters in vocabulary appear in the strings in the list sentences.



      So I want the output to be:



      a = 4
      b = 5
      c = 6


      My program:



      counter = Counter()
      for word in sentences:
      if word in vocabulary:
      counter.update(word)
      print(counter)


      But I keep getting the output:



      Counter()






      python string python-3.x dictionary counter






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 13:31









      jpp

      97.4k2159109




      97.4k2159109










      asked Nov 13 '18 at 13:24









      JameshGongJameshGong

      486




      486
























          3 Answers
          3






          active

          oldest

          votes


















          2














          Counter is a subclass of dict. dict.update accepts another dictionary or an iterable of pairs. But you're only supplying a single character.



          In this case, you can chain your list of strings and pass to Counter, then filter the result via a dictionary comprehension:



          from collections import Counter
          from itertools import chain

          vocabulary = ['a','b','c']
          sentences = ['a a b b c c', 'a c b c', 'b c c a b']

          vocab_set = set(vocabulary)
          c = Counter(chain.from_iterable(sentences))
          res = {k: v for k, v in c.items() if k in vocab_set}

          {'a': 4, 'b': 5, 'c': 6}





          share|improve this answer































            1














            This will do it, no import needed:



            vocabulary = ['a','b','c']
            sentences = ['a a b b c c', 'a c b c', 'b c c a b']

            data = ''.join(sentences)

            for v in vocabulary:
            print('{}: {}'.format(v, data.count(v)))

            a: 4
            b: 5
            c: 6





            share|improve this answer
























            • Worth noting this is at the cost of time complexity O(m * n) vs O(n).

              – jpp
              Nov 13 '18 at 13:33



















            1














            An O(n) solution, with no import:



            vocabulary = ['a', 'b', 'c']
            sentences = ['a a b b c c', 'a c b c', 'b c c a b']

            counts = {}
            vocab_set = set(vocabulary)
            for sentence in sentences:
            for ch in sentence:
            if ch in vocab_set:
            counts[ch] = counts.get(ch, 0) + 1

            print(counts)


            Output



            {'c': 6, 'a': 4, 'b': 5}





            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%2f53282019%2fcheck-if-a-list-of-strings-contains-letters-from-another-list%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              Counter is a subclass of dict. dict.update accepts another dictionary or an iterable of pairs. But you're only supplying a single character.



              In this case, you can chain your list of strings and pass to Counter, then filter the result via a dictionary comprehension:



              from collections import Counter
              from itertools import chain

              vocabulary = ['a','b','c']
              sentences = ['a a b b c c', 'a c b c', 'b c c a b']

              vocab_set = set(vocabulary)
              c = Counter(chain.from_iterable(sentences))
              res = {k: v for k, v in c.items() if k in vocab_set}

              {'a': 4, 'b': 5, 'c': 6}





              share|improve this answer




























                2














                Counter is a subclass of dict. dict.update accepts another dictionary or an iterable of pairs. But you're only supplying a single character.



                In this case, you can chain your list of strings and pass to Counter, then filter the result via a dictionary comprehension:



                from collections import Counter
                from itertools import chain

                vocabulary = ['a','b','c']
                sentences = ['a a b b c c', 'a c b c', 'b c c a b']

                vocab_set = set(vocabulary)
                c = Counter(chain.from_iterable(sentences))
                res = {k: v for k, v in c.items() if k in vocab_set}

                {'a': 4, 'b': 5, 'c': 6}





                share|improve this answer


























                  2












                  2








                  2







                  Counter is a subclass of dict. dict.update accepts another dictionary or an iterable of pairs. But you're only supplying a single character.



                  In this case, you can chain your list of strings and pass to Counter, then filter the result via a dictionary comprehension:



                  from collections import Counter
                  from itertools import chain

                  vocabulary = ['a','b','c']
                  sentences = ['a a b b c c', 'a c b c', 'b c c a b']

                  vocab_set = set(vocabulary)
                  c = Counter(chain.from_iterable(sentences))
                  res = {k: v for k, v in c.items() if k in vocab_set}

                  {'a': 4, 'b': 5, 'c': 6}





                  share|improve this answer













                  Counter is a subclass of dict. dict.update accepts another dictionary or an iterable of pairs. But you're only supplying a single character.



                  In this case, you can chain your list of strings and pass to Counter, then filter the result via a dictionary comprehension:



                  from collections import Counter
                  from itertools import chain

                  vocabulary = ['a','b','c']
                  sentences = ['a a b b c c', 'a c b c', 'b c c a b']

                  vocab_set = set(vocabulary)
                  c = Counter(chain.from_iterable(sentences))
                  res = {k: v for k, v in c.items() if k in vocab_set}

                  {'a': 4, 'b': 5, 'c': 6}






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 13:30









                  jppjpp

                  97.4k2159109




                  97.4k2159109

























                      1














                      This will do it, no import needed:



                      vocabulary = ['a','b','c']
                      sentences = ['a a b b c c', 'a c b c', 'b c c a b']

                      data = ''.join(sentences)

                      for v in vocabulary:
                      print('{}: {}'.format(v, data.count(v)))

                      a: 4
                      b: 5
                      c: 6





                      share|improve this answer
























                      • Worth noting this is at the cost of time complexity O(m * n) vs O(n).

                        – jpp
                        Nov 13 '18 at 13:33
















                      1














                      This will do it, no import needed:



                      vocabulary = ['a','b','c']
                      sentences = ['a a b b c c', 'a c b c', 'b c c a b']

                      data = ''.join(sentences)

                      for v in vocabulary:
                      print('{}: {}'.format(v, data.count(v)))

                      a: 4
                      b: 5
                      c: 6





                      share|improve this answer
























                      • Worth noting this is at the cost of time complexity O(m * n) vs O(n).

                        – jpp
                        Nov 13 '18 at 13:33














                      1












                      1








                      1







                      This will do it, no import needed:



                      vocabulary = ['a','b','c']
                      sentences = ['a a b b c c', 'a c b c', 'b c c a b']

                      data = ''.join(sentences)

                      for v in vocabulary:
                      print('{}: {}'.format(v, data.count(v)))

                      a: 4
                      b: 5
                      c: 6





                      share|improve this answer













                      This will do it, no import needed:



                      vocabulary = ['a','b','c']
                      sentences = ['a a b b c c', 'a c b c', 'b c c a b']

                      data = ''.join(sentences)

                      for v in vocabulary:
                      print('{}: {}'.format(v, data.count(v)))

                      a: 4
                      b: 5
                      c: 6






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 13 '18 at 13:30









                      zipazipa

                      16k31637




                      16k31637













                      • Worth noting this is at the cost of time complexity O(m * n) vs O(n).

                        – jpp
                        Nov 13 '18 at 13:33



















                      • Worth noting this is at the cost of time complexity O(m * n) vs O(n).

                        – jpp
                        Nov 13 '18 at 13:33

















                      Worth noting this is at the cost of time complexity O(m * n) vs O(n).

                      – jpp
                      Nov 13 '18 at 13:33





                      Worth noting this is at the cost of time complexity O(m * n) vs O(n).

                      – jpp
                      Nov 13 '18 at 13:33











                      1














                      An O(n) solution, with no import:



                      vocabulary = ['a', 'b', 'c']
                      sentences = ['a a b b c c', 'a c b c', 'b c c a b']

                      counts = {}
                      vocab_set = set(vocabulary)
                      for sentence in sentences:
                      for ch in sentence:
                      if ch in vocab_set:
                      counts[ch] = counts.get(ch, 0) + 1

                      print(counts)


                      Output



                      {'c': 6, 'a': 4, 'b': 5}





                      share|improve this answer






























                        1














                        An O(n) solution, with no import:



                        vocabulary = ['a', 'b', 'c']
                        sentences = ['a a b b c c', 'a c b c', 'b c c a b']

                        counts = {}
                        vocab_set = set(vocabulary)
                        for sentence in sentences:
                        for ch in sentence:
                        if ch in vocab_set:
                        counts[ch] = counts.get(ch, 0) + 1

                        print(counts)


                        Output



                        {'c': 6, 'a': 4, 'b': 5}





                        share|improve this answer




























                          1












                          1








                          1







                          An O(n) solution, with no import:



                          vocabulary = ['a', 'b', 'c']
                          sentences = ['a a b b c c', 'a c b c', 'b c c a b']

                          counts = {}
                          vocab_set = set(vocabulary)
                          for sentence in sentences:
                          for ch in sentence:
                          if ch in vocab_set:
                          counts[ch] = counts.get(ch, 0) + 1

                          print(counts)


                          Output



                          {'c': 6, 'a': 4, 'b': 5}





                          share|improve this answer















                          An O(n) solution, with no import:



                          vocabulary = ['a', 'b', 'c']
                          sentences = ['a a b b c c', 'a c b c', 'b c c a b']

                          counts = {}
                          vocab_set = set(vocabulary)
                          for sentence in sentences:
                          for ch in sentence:
                          if ch in vocab_set:
                          counts[ch] = counts.get(ch, 0) + 1

                          print(counts)


                          Output



                          {'c': 6, 'a': 4, 'b': 5}






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 13 '18 at 13:52

























                          answered Nov 13 '18 at 13:43









                          Daniel MesejoDaniel Mesejo

                          16.5k21430




                          16.5k21430






























                              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%2f53282019%2fcheck-if-a-list-of-strings-contains-letters-from-another-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

                              Florida Star v. B. J. F.

                              Danny Elfman

                              Lugert, Oklahoma