Return-Path: 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; 'cc:addr:python-list': 0.11; '(2,': 0.16; '(3,': 0.16; '1):': 0.16; 'collections': 0.16; 'counter()': 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; 'appears': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'looks': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'message- id:@mail.gmail.com': 0.30; '-0700,': 0.31; 'url:python': 0.33; 'common': 0.35; 'received:google.com': 0.35; 'sequence': 0.36; 'url:org': 0.36; 'list': 0.37; 'url:library': 0.38; 'most': 0.60; 'url:3': 0.61; 'here:': 0.62; 'such': 0.63; 'more': 0.64; 'to:addr:gmail.com': 0.65; '(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=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=vpd+jlttyXWtdttDlw57jCFfVutHYcslSwtPE01VY3c=; b=I2YJ9qxH4YH4HyoVDyisDp9VIa2L4yzoGGU+jaeyfcUPHBQiygLgwTPzPLITV6b4r/ RJhq6bGqytm7wLjSayziCVahGUIfHGV6LQOcddT498paRKneo72142MfsQ44P1HrXR+9 JKBz7GN9t9CTZQ75RirorurnJQgT5JvcxlaDiqTjr4nTrQq6QRkpzkVtbnaxGWNwObGw spNs0KkrBI5Eek8rkifXfME10cSkY+WHOb9dtd7IwmD+gSg+3kdxtGZDyx2nMpveK/p/ ojZyP4snBSEllYJKIq7+v9HsWtq6eWo6N30TolhInJsWa5qM1Zw33v8zzJLlJKG1iR9N q9bQ== MIME-Version: 1.0 X-Received: by 10.15.26.6 with SMTP id m6mr47144320eeu.4.1366938971466; Thu, 25 Apr 2013 18:16:11 -0700 (PDT) In-Reply-To: References: Date: Thu, 25 Apr 2013 19:16:11 -0600 Subject: Re: Pythonic way to count sequences From: Modulok To: Denis McMahon Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366938979 news.xs4all.nl 15948 [2001:888:2000:d::a6]:54844 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!news.stben.net!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:44374 On 4/25/13, Denis McMahon 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-