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


Groups > comp.lang.python > #105432

Re: Convert list to another form but providing same information

From Paul Rubin <no.email@nospam.invalid>
Newsgroups comp.lang.python
Subject Re: Convert list to another form but providing same information
Date 2016-03-21 18:35 -0700
Organization A noiseless patient Spider
Message-ID <87twjzz44v.fsf@jester.gateway.pace.com> (permalink)
References <1010f2cb-21f9-495b-8af4-03ad209b4c1e@googlegroups.com>

Show all headers | 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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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