Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44321
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.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 | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.007 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'string.': 0.05; 'assigning': 0.09; 'iterate': 0.09; '%r"': 0.16; 'collections': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'mylist': 0.16; 'tally': 0.16; 'tuple': 0.16; 'two-digit': 0.16; 'variable.': 0.16; 'elements': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'library': 0.18; 'thu,': 0.19; 'appears': 0.22; 'import': 0.22; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; '25,': 0.31; 'skip:c 30': 0.32; 'common': 0.35; 'received:google.com': 0.35; 'sequence': 0.36; 'doing': 0.36; 'list': 0.37; 'ahead': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'simply': 0.61; 'such': 0.63; '(here': 0.84; 'results,': 0.84; '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:content-type; bh=rhjlkWwGxx10nxqAnfXwQT44fS5TqkuD/IPxlx9M+3E=; b=HtilLtW1OJsBZeSJBiMc3U4WvsT7iogKGeeVE8PhcbJQqJzSOqJaVC8pZ5UtM0/Wyy 0L9Fk0wqPVUFZiWDSTxVd41Z6nk/EcmD9bE3lZWwaSZDfDDt6jFSBhwk5WUy6iVY0BKQ kt8FmfeftVVlZ5P1mdUG19mbX1wiGKwTgmMj+HyDHXOLxJIhW2qltpMtERviSl6x8XVM x9NIwoXcDZpTMpz5KT3Vz6Oj+asO8CrtLNZlFzKOuC10+vEq5G53lOImjNq0nl4DSWUp BDvKaCC86ubmxA2kxB1WG0X8FsV62RNuklac14G4rsrD3IW0nZhiMX4+28pQmkDdoiyu TGFg== |
| MIME-Version | 1.0 |
| X-Received | by 10.52.91.230 with SMTP id ch6mr5294012vdb.42.1366867608802; Wed, 24 Apr 2013 22:26:48 -0700 (PDT) |
| In-Reply-To | <bfc57a5f-bbcb-46a4-b59a-e7fa55527da1@y12g2000yqb.googlegroups.com> |
| References | <bfc57a5f-bbcb-46a4-b59a-e7fa55527da1@y12g2000yqb.googlegroups.com> |
| Date | Thu, 25 Apr 2013 15:26:48 +1000 |
| Subject | Re: Pythonic way to count sequences |
| From | Chris Angelico <rosuav@gmail.com> |
| To | python-list@python.org |
| Content-Type | text/plain; charset=ISO-8859-1 |
| 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.1051.1366867611.3114.python-list@python.org> (permalink) |
| Lines | 30 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1366867611 news.xs4all.nl 15879 [2001:888:2000:d::a6]:39393 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:44321 |
Show key headers only | View raw
On Thu, Apr 25, 2013 at 3:05 PM, CM <cmpython@gmail.com> 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.
You can use a tuple as a dictionary key, just like you would a string.
So you can count them up directly with a dictionary:
count = {}
for sequence_tuple in list_of_tuples:
count[sequence_tuple] = count.get(sequence_tuple,0) + 1
Also, since this is such a common thing to do, there's a standard
library way of doing it:
import collections
count = collections.Counter(list_of_tuples)
This doesn't depend on knowing ahead of time what your elements will
be. At the end of it, you can simply iterate over 'count' and get all
your counts:
for sequence,number in count.items():
print("%d of %r" % (number,sequence))
ChrisA
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