Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.python Subject: Re: Convert list to another form but providing same information Date: Mon, 21 Mar 2016 20:03:06 +0000 Organization: A noiseless patient Spider Lines: 37 Message-ID: <87bn67oayt.fsf@bsb.me.uk> References: <1010f2cb-21f9-495b-8af4-03ad209b4c1e@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="10175"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+CbCyz04d/EAJD+x988awZbej0C0QTpYk=" Cancel-Lock: sha1:GCT88K7dxq0CNc3PET/QupF3fnc= sha1:I4lzhRD0W6dtvy3+cR0AAMq/wxU= X-BSB-Auth: 1.498084bcfb3b683d0b7f.20160321200306GMT.87bn67oayt.fsf@bsb.me.uk Xref: csiph.com comp.lang.python:105401 Maurice writes: > Hello, hope everything is okay. I think someone might have dealt with > a similar issue I'm having. > > Basically I wanna do the following: > > I have a list such [6,19,19,21,21,21] (FYI this is the item of a >certain key in the dictionary) > > And I need to convert it to a list of 32 elements (meaning days of the > month however first element ie index 0 or day zero has no meaning - > keeping like that for simplicity's sake). > Therefore the resulting list should be: > [0,0,0,0,0,0,1,0,0,0...,2,0,3,0...0] How about reduce(lambda counts, day: counts[:day] + [counts[day]+1] + counts[day+1:], days, [0]*32) ? (reduce is in functools). Not efficient, but sometimes you just want to the job done. More efficient would be: def inc_day(counts, day): counts[day] += 1; return counts reduce(inc_day, days, [0]*32) For experts here: why can't I write a lambda that has a statement in it (actually I wanted two: lambda l, i: l[i] += 1; return l)? -- Ben.