Re: Convert list to another form but providing same information

2016-03-22 Thread anantguptadbl
On Tuesday, March 22, 2016 at 12:01:10 AM UTC+5:30, Maurice wrote: > Just figured why: > > If I type this on the kernel: > > weirdList = [[0]*3]*5 > > weirdList > Out[257]: [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] > > weirdList[0][0] = 1 > > weirdList > Out[259]: [[1, 0, 0], [1,

Re: Convert list to another form but providing same information

2016-03-22 Thread Steven D'Aprano
On Tue, 22 Mar 2016 12:35 pm, Paul Rubin wrote: > Maurice 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

Re: Convert list to another form but providing same information

2016-03-21 Thread Steven D'Aprano
On Tuesday 22 March 2016 11:31, Ben Bacarisse wrote: > Ian Kelly writes: > >> On Mon, Mar 21, 2016 at 2:03 PM, Ben Bacarisse >> wrote: >>> 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)? >> >> https://docs.py

Re: Convert list to another form but providing same information

2016-03-21 Thread Paul Rubin
Maurice 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]

Re: Convert list to another form but providing same information

2016-03-21 Thread Chris Angelico
On Tue, Mar 22, 2016 at 11:31 AM, Ben Bacarisse wrote: > However, the explanation ("because Python’s syntactic framework can't > handle statements nested inside expressions") seemed, at first, to be > saying you can't because you can't! But the term "syntactic framework" > hints that it's not rea

Re: Convert list to another form but providing same information

2016-03-21 Thread Ben Bacarisse
Ian Kelly writes: > On Mon, Mar 21, 2016 at 2:03 PM, Ben Bacarisse wrote: >> 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)? > > https://docs.python.org/3/faq/design.html#why-can-t-lambda-expressions-contain-st

Re: Convert list to another form but providing same information

2016-03-21 Thread Steven D'Aprano
On Tue, 22 Mar 2016 05:26 am, Maurice wrote: > 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 - > keepi

Re: Convert list to another form but providing same information

2016-03-21 Thread Ian Kelly
On Mon, Mar 21, 2016 at 2:12 PM, Ian Kelly wrote: > On Mon, Mar 21, 2016 at 2:03 PM, Ben Bacarisse wrote: >> 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)? > > https://docs.python.org/3/faq/design.html#why-can-

Re: Convert list to another form but providing same information

2016-03-21 Thread Ian Kelly
On Mon, Mar 21, 2016 at 2:03 PM, Ben Bacarisse wrote: > 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)? https://docs.python.org/3/faq/design.html#why-can-t-lambda-expressions-contain-statements -- https://mail.p

Re: Convert list to another form but providing same information

2016-03-21 Thread Ben Bacarisse
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

Re: Convert list to another form but providing same information

2016-03-21 Thread Mark Lawrence
On 21/03/2016 18:30, Maurice wrote: Just figured why: If I type this on the kernel: weirdList = [[0]*3]*5 weirdList Out[257]: [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] weirdList[0][0] = 1 weirdList Out[259]: [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]] All first ele

Re: Convert list to another form but providing same information

2016-03-21 Thread Peter Otten
Maurice wrote: > 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 li

Re: Convert list to another form but providing same information

2016-03-21 Thread Mark Lawrence
On 21/03/2016 18:26, Maurice wrote: 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 t

Re: Convert list to another form but providing same information

2016-03-21 Thread Maurice
Just figured why: If I type this on the kernel: weirdList = [[0]*3]*5 weirdList Out[257]: [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] weirdList[0][0] = 1 weirdList Out[259]: [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]] All first elements of the sublists also changes. I

Convert list to another form but providing same information

2016-03-21 Thread Maurice
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