Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #105432
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Paul Rubin <no.email@nospam.invalid> |
| Newsgroups | comp.lang.python |
| Subject | Re: Convert list to another form but providing same information |
| Date | Mon, 21 Mar 2016 18:35:12 -0700 |
| Organization | A noiseless patient Spider |
| Lines | 21 |
| Message-ID | <87twjzz44v.fsf@jester.gateway.pace.com> (permalink) |
| References | <1010f2cb-21f9-495b-8af4-03ad209b4c1e@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain |
| Injection-Info | mx02.eternal-september.org; posting-host="3c7a0b0f75f6c95b248ff930d17c36e7"; logging-data="12413"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+1L9WSbLUkds9YJmZTrjaE" |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) |
| Cancel-Lock | sha1:xRh//jt4lNvy8YDS8Zflp/hwazE= sha1:6jBGJlFpTMh5wTOWEoXrqUr0li4= |
| Xref | csiph.com comp.lang.python:105432 |
Show key headers only | View raw
Maurice <mauricioliveiraguarda@gmail.com> writes:
> I have a list such [6,19,19,21,21,21]
> Therefore the resulting list should be:
> [0,0,0,0,0,0,1,0,0,0...,2,0,3,0...0]
Rather than a sparse list you'd typically want a dictionary (untested):
from collections import defaultdict
the_list = [0,0,0,0,0,0,1,0,0,0...,2,0,3,0...0]
...
days = defaultdict(int)
for t in the_list:
days[t] += 1
this results in days being the defaultdict { 6:1, 19:2, 21:3 }.
defaultdict is a special type of dictionary where if you try to access a
non-existent element, it gets created and initialized with the data
constructor you made it with. In this case the data constructor is int,
and int() makes the number 0, so the defaultdict elements are
initialized to 0.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Convert list to another form but providing same information Maurice <mauricioliveiraguarda@gmail.com> - 2016-03-21 11:26 -0700
Re: Convert list to another form but providing same information Maurice <mauricioliveiraguarda@gmail.com> - 2016-03-21 11:30 -0700
Re: Convert list to another form but providing same information Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-21 19:24 +0000
Re: Convert list to another form but providing same information anantguptadbl@gmail.com - 2016-03-22 07:23 -0700
Re: Convert list to another form but providing same information Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-21 19:21 +0000
Re: Convert list to another form but providing same information Peter Otten <__peter__@web.de> - 2016-03-21 20:22 +0100
Re: Convert list to another form but providing same information Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-03-21 20:03 +0000
Re: Convert list to another form but providing same information Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-21 14:12 -0600
Re: Convert list to another form but providing same information Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-03-22 00:31 +0000
Re: Convert list to another form but providing same information Chris Angelico <rosuav@gmail.com> - 2016-03-22 11:58 +1100
Re: Convert list to another form but providing same information Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-03-22 15:34 +1100
Re: Convert list to another form but providing same information Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-21 14:14 -0600
Re: Convert list to another form but providing same information Steven D'Aprano <steve@pearwood.info> - 2016-03-22 11:13 +1100
Re: Convert list to another form but providing same information Paul Rubin <no.email@nospam.invalid> - 2016-03-21 18:35 -0700
Re: Convert list to another form but providing same information Steven D'Aprano <steve@pearwood.info> - 2016-03-22 21:49 +1100
csiph-web