Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44374
| Return-Path | <modulok@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; '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 | <klce8o$2pp$4@dont-email.me> |
| References | <bfc57a5f-bbcb-46a4-b59a-e7fa55527da1@y12g2000yqb.googlegroups.com> <klce8o$2pp$4@dont-email.me> |
| Date | Thu, 25 Apr 2013 19:16:11 -0600 |
| Subject | Re: Pythonic way to count sequences |
| From | Modulok <modulok@gmail.com> |
| To | Denis McMahon <denismfmcmahon@gmail.com> |
| 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 <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.1074.1366938979.3114.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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