Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44378

Re: Pythonic way to count sequences

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <m.gilson1@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.015
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'output': 0.05; 'skip:` 10': 0.07; 'assigning': 0.09; '(2,': 0.16; '(3,': 0.16; '1):': 0.16; 'backward': 0.16; 'collections': 0.16; 'counter()': 0.16; 'defaultdict': 0.16; 'luck!': 0.16; 'mylist': 0.16; 'tally': 0.16; 'two-digit': 0.16; 'variable.': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'examples': 0.20; '>>>': 0.22; 'appears': 0.22; 'example': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'looks': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; '-0700,': 0.31; 'this.': 0.32; 'compatible': 0.32; 'url:python': 0.33; 'could': 0.34; 'received:209.85': 0.35; 'common': 0.35; 'received:209.85.220': 0.35; 'received:google.com': 0.35; 'sequence': 0.36; 'url:org': 0.36; 'example,': 0.37; 'list': 0.37; 'received:209': 0.37; 'message-id:@gmail.com': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'little': 0.38; 'to:addr:python.org': 0.39; 'most': 0.60; 'url:3': 0.61; 'here:': 0.62; 'such': 0.63; 'more': 0.64; '(here': 0.84; 'results,': 0.84; 'url:counter': 0.84; 'items,': 0.91; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:message-id:date:from:user-agent:mime-version:to:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=P2NSVIilNNwUv5O7h/6aCX3n0m8bEZgDVyk8+xGTdOQ=; b=BfOrEaXV2G2pJZOp40lh+kX5BZ3RIeVdpT6KGe1FRHr01MnETIuqhamkv0aAIkB+NZ 7JAncMtCD8hIXRyGjFp5evGHy6SYU70uxFtpmKYSoztTX2c/mlcE5aGfdne3PyDgglze jwUgtkaRdZ59xM/06fp4OMIfCltclra8bejd9fesL6wSUUJqs6bJFysUgb8uGbQaU2h8 oHj9FsVi/HRYOXKUt0IXMeMtzPKHOTNo7PzrY7bHck/9XwRTwvCNidJnz48f0bGIZxLJ y1NEaJevit1cw8BNzJkLl8QsakzE1OdbOxUXh+LZcjkJ4T9+O8Dsempp32fTdjE479Nt HZFw==
X-Received by 10.66.72.3 with SMTP id z3mr44334356pau.125.1366944039474; Thu, 25 Apr 2013 19:40:39 -0700 (PDT)
Date Thu, 25 Apr 2013 22:40:36 -0400
From Matthew Gilson <m.gilson1@gmail.com>
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.5; rv:14.0) Gecko/20120713 Thunderbird/14.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Pythonic way to count sequences
References <bfc57a5f-bbcb-46a4-b59a-e7fa55527da1@y12g2000yqb.googlegroups.com> <klce8o$2pp$4@dont-email.me> <CAN2+EpagEq6PzbCXpxP5s62WyzsCJUa4EDvtD7ockNav8YqxkA@mail.gmail.com>
In-Reply-To <CAN2+EpagEq6PzbCXpxP5s62WyzsCJUa4EDvtD7ockNav8YqxkA@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1076.1366944043.3114.python-list@python.org> (permalink)
Lines 55
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1366944043 news.xs4all.nl 15997 [2001:888:2000:d::a6]:53885
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:44378

Show key headers only | View raw


A Counter is definitely the way to go about this.  Just as a little more 
information.  The below example can be simplified:

     from collections import Counter
     count = Counter(mylist)

With the other example, you could have achieved the same thing (and been 
backward compatible to python2.5) with

    from collections import defaultdict
    count = defaultdict(int)
    for k in mylist:
         count[k] += 1



On 4/25/13 9:16 PM, Modulok wrote:
> On 4/25/13, Denis McMahon <denismfmcmahon@gmail.com> wrote:
>> On Wed, 24 Apr 2013 22:05:52 -0700, CM wrote:
>>
>>> I have to count the number of various two-digit sequences in a list such
>>> as this:
>>>
>>> mylist = [(2,4), (2,4), (3,4), (4,5), (2,1)]  # (Here the (2,4) sequence
>>> appears 2 times.)
>>>
>>> and tally up the results, assigning each to a variable.
> ...
>
> Consider using the ``collections`` module::
>
>
>      from collections import Counter
>
>      mylist = [(2,4), (2,4), (3,4), (4,5), (2,1)]
>      count = Counter()
>      for k in mylist:
>          count[k] += 1
>
>      print(count)
>
>      # Output looks like this:
>      # Counter({(2, 4): 2, (4, 5): 1, (3, 4): 1, (2, 1): 1})
>
>
> You then have access to methods to return the most common items, etc. See more
> examples here:
>
> http://docs.python.org/3.3/library/collections.html#collections.Counter
>
>
> Good luck!
> -Modulok-

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Pythonic way to count sequences CM <cmpython@gmail.com> - 2013-04-24 22:05 -0700
  Re: Pythonic way to count sequences Chris Angelico <rosuav@gmail.com> - 2013-04-25 15:26 +1000
    Re: Pythonic way to count sequences CM <cmpython@gmail.com> - 2013-04-25 19:40 -0700
  Re: Pythonic way to count sequences Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-25 06:14 +0000
  Re: Pythonic way to count sequences Serhiy Storchaka <storchaka@gmail.com> - 2013-04-25 15:36 +0300
  Re: Pythonic way to count sequences Denis McMahon <denismfmcmahon@gmail.com> - 2013-04-25 23:29 +0000
    Re: Pythonic way to count sequences Modulok <modulok@gmail.com> - 2013-04-25 19:16 -0600
    Re: Pythonic way to count sequences Matthew Gilson <m.gilson1@gmail.com> - 2013-04-25 22:40 -0400

csiph-web