How to make subset of Counter?











up vote
2
down vote

favorite












I am experimenting with Python standard library Collections.



I have a Counter of things as



>>> c = Counter('achdnsdsdsdsdklaffaefhaew')
>>> c
Counter({'a': 4,
'c': 1,
'h': 2,
'd': 5,
'n': 1,
's': 4,
'k': 1,
'l': 1,
'f': 3,
'e': 2,
'w': 1})


What I want now is to somehow get subset of this counter as another Counter object. Just like this:



>>> new_c = do_subset(c, [d,s,l,e,w])
>>> new_c
Counter({'d': 5,
's': 4,
'l': 1,
'e': 2,
'w': 1})


Thank you in advance.










share|improve this question


























    up vote
    2
    down vote

    favorite












    I am experimenting with Python standard library Collections.



    I have a Counter of things as



    >>> c = Counter('achdnsdsdsdsdklaffaefhaew')
    >>> c
    Counter({'a': 4,
    'c': 1,
    'h': 2,
    'd': 5,
    'n': 1,
    's': 4,
    'k': 1,
    'l': 1,
    'f': 3,
    'e': 2,
    'w': 1})


    What I want now is to somehow get subset of this counter as another Counter object. Just like this:



    >>> new_c = do_subset(c, [d,s,l,e,w])
    >>> new_c
    Counter({'d': 5,
    's': 4,
    'l': 1,
    'e': 2,
    'w': 1})


    Thank you in advance.










    share|improve this question
























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I am experimenting with Python standard library Collections.



      I have a Counter of things as



      >>> c = Counter('achdnsdsdsdsdklaffaefhaew')
      >>> c
      Counter({'a': 4,
      'c': 1,
      'h': 2,
      'd': 5,
      'n': 1,
      's': 4,
      'k': 1,
      'l': 1,
      'f': 3,
      'e': 2,
      'w': 1})


      What I want now is to somehow get subset of this counter as another Counter object. Just like this:



      >>> new_c = do_subset(c, [d,s,l,e,w])
      >>> new_c
      Counter({'d': 5,
      's': 4,
      'l': 1,
      'e': 2,
      'w': 1})


      Thank you in advance.










      share|improve this question













      I am experimenting with Python standard library Collections.



      I have a Counter of things as



      >>> c = Counter('achdnsdsdsdsdklaffaefhaew')
      >>> c
      Counter({'a': 4,
      'c': 1,
      'h': 2,
      'd': 5,
      'n': 1,
      's': 4,
      'k': 1,
      'l': 1,
      'f': 3,
      'e': 2,
      'w': 1})


      What I want now is to somehow get subset of this counter as another Counter object. Just like this:



      >>> new_c = do_subset(c, [d,s,l,e,w])
      >>> new_c
      Counter({'d': 5,
      's': 4,
      'l': 1,
      'e': 2,
      'w': 1})


      Thank you in advance.







      python data-structures python-collections






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 2:08









      Jay Patel

      463




      463
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          You could simply build a dictionary and pass it to Counter:



          from collections import Counter

          c = Counter({'a': 4,
          'c': 1,
          'h': 2,
          'd': 5,
          'n': 1,
          's': 4,
          'k': 1,
          'l': 1,
          'f': 3,
          'e': 2,
          'w': 1})


          def do_subset(counter, lst):
          return Counter({k: counter.get(k, 0) for k in lst})


          result = do_subset(c, ['d', 's', 'l', 'e', 'w'])

          print(result)


          Output



          Counter({'d': 5, 's': 4, 'e': 2, 'l': 1, 'w': 1})





          share|improve this answer

















          • 2




            If you are lazy, you can also pass 'dslew' because strings are iterable.
            – jpp
            Nov 11 at 2:14










          • Thank you @daniel it worked.
            – Jay Patel
            Nov 11 at 2:17












          • @jpp you made it shorter... awesome...
            – Jay Patel
            Nov 11 at 2:18










          • @JayPatel Glad I could help! If my answer helped to solve your problem, please consider marking it as accepted. That's the customary way of indicating that your question is "resolved" and thanking the person who helped you.
            – Daniel Mesejo
            Nov 11 at 2:20




















          up vote
          0
          down vote













          You can access each key in c and assign its value to the same key in a new dict.



          import collections
          c = collections.Counter('achdnsdsdsdsdklaffaefhaew')

          def subsetter(c, sub):
          out = {}
          for x in sub:
          out[x] = c[x]
          return collections.Counter(out)

          subsetter(c, ["d","s","l","e","w"])


          Yields:



          {'d': 5, 'e': 2, 'l': 1, 's': 4, 'w': 1}





          share|improve this answer























          • OP wants a Counter not a dict.
            – Red Cricket
            Nov 11 at 2:19











          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',
          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%2f53245239%2fhow-to-make-subset-of-counter%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          You could simply build a dictionary and pass it to Counter:



          from collections import Counter

          c = Counter({'a': 4,
          'c': 1,
          'h': 2,
          'd': 5,
          'n': 1,
          's': 4,
          'k': 1,
          'l': 1,
          'f': 3,
          'e': 2,
          'w': 1})


          def do_subset(counter, lst):
          return Counter({k: counter.get(k, 0) for k in lst})


          result = do_subset(c, ['d', 's', 'l', 'e', 'w'])

          print(result)


          Output



          Counter({'d': 5, 's': 4, 'e': 2, 'l': 1, 'w': 1})





          share|improve this answer

















          • 2




            If you are lazy, you can also pass 'dslew' because strings are iterable.
            – jpp
            Nov 11 at 2:14










          • Thank you @daniel it worked.
            – Jay Patel
            Nov 11 at 2:17












          • @jpp you made it shorter... awesome...
            – Jay Patel
            Nov 11 at 2:18










          • @JayPatel Glad I could help! If my answer helped to solve your problem, please consider marking it as accepted. That's the customary way of indicating that your question is "resolved" and thanking the person who helped you.
            – Daniel Mesejo
            Nov 11 at 2:20

















          up vote
          4
          down vote



          accepted










          You could simply build a dictionary and pass it to Counter:



          from collections import Counter

          c = Counter({'a': 4,
          'c': 1,
          'h': 2,
          'd': 5,
          'n': 1,
          's': 4,
          'k': 1,
          'l': 1,
          'f': 3,
          'e': 2,
          'w': 1})


          def do_subset(counter, lst):
          return Counter({k: counter.get(k, 0) for k in lst})


          result = do_subset(c, ['d', 's', 'l', 'e', 'w'])

          print(result)


          Output



          Counter({'d': 5, 's': 4, 'e': 2, 'l': 1, 'w': 1})





          share|improve this answer

















          • 2




            If you are lazy, you can also pass 'dslew' because strings are iterable.
            – jpp
            Nov 11 at 2:14










          • Thank you @daniel it worked.
            – Jay Patel
            Nov 11 at 2:17












          • @jpp you made it shorter... awesome...
            – Jay Patel
            Nov 11 at 2:18










          • @JayPatel Glad I could help! If my answer helped to solve your problem, please consider marking it as accepted. That's the customary way of indicating that your question is "resolved" and thanking the person who helped you.
            – Daniel Mesejo
            Nov 11 at 2:20















          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          You could simply build a dictionary and pass it to Counter:



          from collections import Counter

          c = Counter({'a': 4,
          'c': 1,
          'h': 2,
          'd': 5,
          'n': 1,
          's': 4,
          'k': 1,
          'l': 1,
          'f': 3,
          'e': 2,
          'w': 1})


          def do_subset(counter, lst):
          return Counter({k: counter.get(k, 0) for k in lst})


          result = do_subset(c, ['d', 's', 'l', 'e', 'w'])

          print(result)


          Output



          Counter({'d': 5, 's': 4, 'e': 2, 'l': 1, 'w': 1})





          share|improve this answer












          You could simply build a dictionary and pass it to Counter:



          from collections import Counter

          c = Counter({'a': 4,
          'c': 1,
          'h': 2,
          'd': 5,
          'n': 1,
          's': 4,
          'k': 1,
          'l': 1,
          'f': 3,
          'e': 2,
          'w': 1})


          def do_subset(counter, lst):
          return Counter({k: counter.get(k, 0) for k in lst})


          result = do_subset(c, ['d', 's', 'l', 'e', 'w'])

          print(result)


          Output



          Counter({'d': 5, 's': 4, 'e': 2, 'l': 1, 'w': 1})






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 2:12









          Daniel Mesejo

          8,3791923




          8,3791923








          • 2




            If you are lazy, you can also pass 'dslew' because strings are iterable.
            – jpp
            Nov 11 at 2:14










          • Thank you @daniel it worked.
            – Jay Patel
            Nov 11 at 2:17












          • @jpp you made it shorter... awesome...
            – Jay Patel
            Nov 11 at 2:18










          • @JayPatel Glad I could help! If my answer helped to solve your problem, please consider marking it as accepted. That's the customary way of indicating that your question is "resolved" and thanking the person who helped you.
            – Daniel Mesejo
            Nov 11 at 2:20
















          • 2




            If you are lazy, you can also pass 'dslew' because strings are iterable.
            – jpp
            Nov 11 at 2:14










          • Thank you @daniel it worked.
            – Jay Patel
            Nov 11 at 2:17












          • @jpp you made it shorter... awesome...
            – Jay Patel
            Nov 11 at 2:18










          • @JayPatel Glad I could help! If my answer helped to solve your problem, please consider marking it as accepted. That's the customary way of indicating that your question is "resolved" and thanking the person who helped you.
            – Daniel Mesejo
            Nov 11 at 2:20










          2




          2




          If you are lazy, you can also pass 'dslew' because strings are iterable.
          – jpp
          Nov 11 at 2:14




          If you are lazy, you can also pass 'dslew' because strings are iterable.
          – jpp
          Nov 11 at 2:14












          Thank you @daniel it worked.
          – Jay Patel
          Nov 11 at 2:17






          Thank you @daniel it worked.
          – Jay Patel
          Nov 11 at 2:17














          @jpp you made it shorter... awesome...
          – Jay Patel
          Nov 11 at 2:18




          @jpp you made it shorter... awesome...
          – Jay Patel
          Nov 11 at 2:18












          @JayPatel Glad I could help! If my answer helped to solve your problem, please consider marking it as accepted. That's the customary way of indicating that your question is "resolved" and thanking the person who helped you.
          – Daniel Mesejo
          Nov 11 at 2:20






          @JayPatel Glad I could help! If my answer helped to solve your problem, please consider marking it as accepted. That's the customary way of indicating that your question is "resolved" and thanking the person who helped you.
          – Daniel Mesejo
          Nov 11 at 2:20














          up vote
          0
          down vote













          You can access each key in c and assign its value to the same key in a new dict.



          import collections
          c = collections.Counter('achdnsdsdsdsdklaffaefhaew')

          def subsetter(c, sub):
          out = {}
          for x in sub:
          out[x] = c[x]
          return collections.Counter(out)

          subsetter(c, ["d","s","l","e","w"])


          Yields:



          {'d': 5, 'e': 2, 'l': 1, 's': 4, 'w': 1}





          share|improve this answer























          • OP wants a Counter not a dict.
            – Red Cricket
            Nov 11 at 2:19















          up vote
          0
          down vote













          You can access each key in c and assign its value to the same key in a new dict.



          import collections
          c = collections.Counter('achdnsdsdsdsdklaffaefhaew')

          def subsetter(c, sub):
          out = {}
          for x in sub:
          out[x] = c[x]
          return collections.Counter(out)

          subsetter(c, ["d","s","l","e","w"])


          Yields:



          {'d': 5, 'e': 2, 'l': 1, 's': 4, 'w': 1}





          share|improve this answer























          • OP wants a Counter not a dict.
            – Red Cricket
            Nov 11 at 2:19













          up vote
          0
          down vote










          up vote
          0
          down vote









          You can access each key in c and assign its value to the same key in a new dict.



          import collections
          c = collections.Counter('achdnsdsdsdsdklaffaefhaew')

          def subsetter(c, sub):
          out = {}
          for x in sub:
          out[x] = c[x]
          return collections.Counter(out)

          subsetter(c, ["d","s","l","e","w"])


          Yields:



          {'d': 5, 'e': 2, 'l': 1, 's': 4, 'w': 1}





          share|improve this answer














          You can access each key in c and assign its value to the same key in a new dict.



          import collections
          c = collections.Counter('achdnsdsdsdsdklaffaefhaew')

          def subsetter(c, sub):
          out = {}
          for x in sub:
          out[x] = c[x]
          return collections.Counter(out)

          subsetter(c, ["d","s","l","e","w"])


          Yields:



          {'d': 5, 'e': 2, 'l': 1, 's': 4, 'w': 1}






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 2:20

























          answered Nov 11 at 2:12









          Charles Landau

          1,2461212




          1,2461212












          • OP wants a Counter not a dict.
            – Red Cricket
            Nov 11 at 2:19


















          • OP wants a Counter not a dict.
            – Red Cricket
            Nov 11 at 2:19
















          OP wants a Counter not a dict.
          – Red Cricket
          Nov 11 at 2:19




          OP wants a Counter not a dict.
          – Red Cricket
          Nov 11 at 2:19


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245239%2fhow-to-make-subset-of-counter%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

          Full-time equivalent

          Bicuculline

          さくらももこ