Re: list of lists

2018-07-23 Thread Sharan Basappa
On Sunday, 22 July 2018 21:07:17 UTC+5:30, Thomas Jollans wrote: > On 22/07/18 14:53, Sharan Basappa wrote: > > Thanks. I initially thought about this but did not know if this is legal > > syntax. > > In this kind of situation – you think you know how to do something but > you're not quite sure

Re: list of lists

2018-07-22 Thread Thomas Jollans
On 22/07/18 14:53, Sharan Basappa wrote: > Thanks. I initially thought about this but did not know if this is legal > syntax. In this kind of situation – you think you know how to do something but you're not quite sure if it'll work as intended – just try it! Start up an interactive interpreter,

Re: list of lists

2018-07-22 Thread Sharan Basappa
On Sunday, 22 July 2018 18:34:41 UTC+5:30, Iwo Herka wrote: > > Can you tell me how this works? > > "results[0]" returns a list with two elements. Let's call it "pair" > > pair = results[0] > # ['1', 0.99921393753233001] > > Now, we can use regular sequence unpacking to retrieve first a

Re: list of lists

2018-07-22 Thread Iwo Herka
> Can you tell me how this works? "results[0]" returns a list with two elements. Let's call it "pair" pair = results[0] # ['1', 0.99921393753233001] Now, we can use regular sequence unpacking to retrieve first and second argument: a, b = pair which is equivalent to this:

Re: list of lists

2018-07-22 Thread Sharan Basappa
On Sunday, 22 July 2018 18:15:23 UTC+5:30, Frank Millman wrote: > "Sharan Basappa" wrote in message > news:[email protected]... > > > > I am using a third party module that is returning list of lists. > > I am using the example below to illustrate. > > > > 1 r

Re: list of lists

2018-07-22 Thread Frank Millman
"Sharan Basappa" wrote in message news:[email protected]... I am using a third party module that is returning list of lists. I am using the example below to illustrate. 1 results = [['1', 0.99921393753233001]] 2 k = results[0] 3 print k[0] 4 print k[1] Assu

Re: list of lists

2018-07-22 Thread Sharan Basappa
Thanks. This works in my example. Can you tell me how this works? > You can simply unpack the inner list: > > a, b = results[0] > > > Iwo Herka > > ‐‐‐ Original Message ‐‐‐ > > On 22 July 2018 11:47 AM, Sharan Basappa wrote: > > > ​​ > > > > I am using a third party module that

Re: list of lists

2018-07-22 Thread Iwo Herka
You can simply unpack the inner list: a, b = results[0] Iwo Herka ‐‐‐ Original Message ‐‐‐ On 22 July 2018 11:47 AM, Sharan Basappa wrote: > ​​ > > I am using a third party module that is returning list of lists. > > I am using the example below to illustrate. > > 1 results =

Re: List of lists surprising behaviour

2010-06-18 Thread Stephen Hansen
On 6/18/10 8:40 AM, bart.c wrote: > I suppose there are pros and cons to both approaches; copying all the time > at least avoids some of the odd effects and inconsistencies you get using > Python: What inconsistencies? All your examples are perfectly consistent. Its just consistent to different id

Re: List of lists surprising behaviour

2010-06-18 Thread bart.c
"Steven D'Aprano" wrote in message news:[email protected]... On Fri, 18 Jun 2010 12:07:38 +0100, bart.c wrote: (Although I have an issue with the way that that append works. I tried it in another, simpler language (which always does deep copies): L:=(1,2,3) L append:

Re: List of lists surprising behaviour

2010-06-18 Thread Steven D'Aprano
On Fri, 18 Jun 2010 12:07:38 +0100, bart.c wrote: > (Although I have an issue with the way that that append works. I tried > it in another, simpler language (which always does deep copies): > > L:=(1,2,3) > L append:= L > print L > > output: (1,2,3,(1,2,3)) > > which is exactly what I'd expect

Re: List of lists surprising behaviour

2010-06-18 Thread bart.c
Lie Ryan wrote: On 06/18/10 20:00, bart.c wrote: (I don't know if Python allows circular references, but that would give problems anyway: how would you even print out such a list?) Python uses ellipsis to indicate recursive list: a = [1, 2, 3] a.append(a) a [1, 2, 3, [...]] Ok, perhaps w

Re: List of lists surprising behaviour

2010-06-18 Thread Lie Ryan
On 06/18/10 20:00, bart.c wrote: > (I > don't know if Python allows circular references, but that would give > problems anyway: how would you even print out such a list?) Python uses ellipsis to indicate recursive list: >>> a = [1, 2, 3] >>> a.append(a) >>> a [1, 2, 3, [...]] -- http://mail.pyt

Re: List of lists surprising behaviour

2010-06-18 Thread bart.c
Benjamin Kaplan wrote: On Thu, Jun 17, 2010 at 4:20 PM, bart.c wrote: I don't know how Python does things, but an object should either specify a special way of duplicating itself, or lend itself to some standard way of doing so. (So for a list, it's just a question of copying the data in the

Re: List of lists surprising behaviour

2010-06-17 Thread Steven D'Aprano
On Fri, 18 Jun 2010 00:20:30 +0100, bart.c wrote: > The code is clearly trying to set only t[0][0] to 1, not t[1][0] and > t[2][0] as well. Trying to guess the motivation of the person writing code is tricky, but in this case, that's a reasonable assumption. I can't think of any reason why some

Re: List of lists surprising behaviour

2010-06-17 Thread Lie Ryan
On 06/18/10 09:20, bart.c wrote: > > "J Kenneth King" wrote in message > news:[email protected]... >> candide writes: >> >>> Let's the following code : >>> >> t=[[0]*2]*3 >> t >>> [[0, 0], [0, 0], [0, 0]] >> t[0][0]=1 >> t >>> [[1, 0], [1, 0], [1, 0]] >>> >>> Rather s

Re: List of lists surprising behaviour

2010-06-17 Thread rantingrick
On Jun 17, 6:44 pm, Benjamin Kaplan wrote: > It's the recursively duplicating each element that's the problem. How > do you know when to stop? Thats easy, stack overflow! ;-) -- http://mail.python.org/mailman/listinfo/python-list

Re: List of lists surprising behaviour

2010-06-17 Thread Benjamin Kaplan
On Thu, Jun 17, 2010 at 4:20 PM, bart.c wrote: > > "J Kenneth King" wrote in message > news:[email protected]... >> >> candide writes: >> >>> Let's the following code : >>> >> t=[[0]*2]*3 >> t >>> >>> [[0, 0], [0, 0], [0, 0]] >> >> t[0][0]=1 >> t >>> >>> [[1, 0],

Re: List of lists surprising behaviour

2010-06-17 Thread bart.c
"J Kenneth King" wrote in message news:[email protected]... candide writes: Let's the following code : t=[[0]*2]*3 t [[0, 0], [0, 0], [0, 0]] t[0][0]=1 t [[1, 0], [1, 0], [1, 0]] Rather surprising, isn't it ? Not at all, actually. The code is clearly trying to set only

Re: List of lists surprising behaviour

2010-06-17 Thread J Kenneth King
candide writes: > Let's the following code : > t=[[0]*2]*3 t > [[0, 0], [0, 0], [0, 0]] t[0][0]=1 t > [[1, 0], [1, 0], [1, 0]] > > Rather surprising, isn't it ? Not at all, actually. I'd be surprised if the multiplication operator was aware of object constructors. Even arr

Re: List of lists surprising behaviour

2010-06-17 Thread Boris Borcic
candide wrote: So what is the right way to initialize to 0 a 2D array ? Is that way correct : >>> t=[[0 for _ in range(2)] for _ in range(3)] That's overkill :) You can skip the inner loop by using a list display, eg t=[[0,0] for _ in range(3)] It seems there is no more trouble now :

Re: List of lists surprising behaviour

2010-06-17 Thread Matteo Landi
Yes you are. List comprehension makes you create list of lists without reference-sharing. You should also find a recipe about that on the python cookbook. On Thu, Jun 17, 2010 at 12:21 PM, candide wrote: > Let's the following code : > t=[[0]*2]*3 t > [[0, 0], [0, 0], [0, 0]] t[0][0

Re: List of lists surprising behaviour

2010-06-17 Thread Lie Ryan
On 06/17/10 20:21, candide wrote: > Let's the following code : > t=[[0]*2]*3 t > [[0, 0], [0, 0], [0, 0]] t[0][0]=1 t > [[1, 0], [1, 0], [1, 0]] > > Rather surprising, isn't it ? So I suppose all the subarrays reférence > the same array : > id(t[0]), id(t[1]), id(t[2]) >

Re: list of lists of lists ....

2006-07-31 Thread yomgui
thanks for all your answers yomgui yomgui wrote: > > Hi, > > I have a list of data (type A) > my list can includes element of type A or a lists, > these list can includes element of type A or a lists, and so on ... > > is there a simple way to obtain a single list of all the elemets > of type

Re: list of lists of lists ....

2006-07-28 Thread bearophileHUGS
You can use this, fast, gives a tuple: from Tkinter import _flatten as flatten --- The xflatten/flatten version I sometimes use, maybe I can put something similar in the cookbook, but it can be improved a lot (and isrecursive is too much fragile): from pprint import isrecurs

Re: list of lists of lists ....

2006-07-28 Thread Michal Kwiatkowski
faulkner wrote: > ok, so, recursion is just functional programming sugar for a loop. And a loop is a procedural programming sugar for tail recursion. 8-) Cheers, mk -- . o . >> http://joker.linuxstuff.pl << . . o It's easier to get forgiveness for being wrong o o o than forgivenes

Re: list of lists of lists ....

2006-07-28 Thread faulkner
doh. ok, so, recursion is just functional programming sugar for a loop. def get_As(L): checking = [elem for elem in L if isinstance(elem, list)]# the equivalent of elem in recursion all_As = [elem for elem in L if isinstance(elem, A)] while checking: new_checking = [] # al

Re: list of lists of lists ....

2006-07-28 Thread faulkner
recursion. def get_As(L): res = [] for elem in L: if isinstance(elem, A): res.append(elem) elif isinstance(elem, list): res += get_As(elem) return res i also have a Tree class in my rc: http://home.comcast.net/~faulkner612/programming/python/pyth

Re: list of lists of lists ....

2006-07-28 Thread yomgui
I forgot the most important, I am looking for a non recursive method. thanks yomgui yomgui wrote: > > Hi, > > I have a list of data (type A) > my list can includes element of type A or a lists, > these list can includes element of type A or a lists, and so on ... > > is there a simple way to

Re: List of lists of lists of lists...

2006-05-15 Thread Ángel Gutiérrez Rodríguez
Robert Kern wrote: > James Stroud wrote: > http://numeric.scipy.org > Thanks! That's anotehr solution, yes! -- Ángel Gutiérrez Rodríguez - [EMAIL PROTECTED] Instituto de Ciencia de los Materiales de Madrid - CSIC SpLine - European Syncrothorn Radiation Facility - Grenoble - France Postal adress:

Re: List of lists of lists of lists...

2006-05-15 Thread Ángel Gutiérrez Rodríguez
bruno at modulix wrote: > for N: > mylist = [mylist] > Right that! > I'm afraid I don't understand. Could you forgive my stupidity and > re-explain this a bit more clearly ? > No need to. Former solution worked fine. Thanks! -- Ángel Gutiérrez Rodríguez - [EMAIL PROTECTED] Instituto de Cienc

Re: List of lists of lists of lists...

2006-05-09 Thread Robert Kern
James Stroud wrote: > Numarray does this sort of thing, but you have to familiarize yourself > with its indexing conventions: > > py> import numarray > py> numarray.ones((3,2)) > array([[1, 1], > [1, 1], > [1, 1]]) > py> numarray.ones((1,2,3)) > array([[[1, 1, 1], > [1,

Re: List of lists of lists of lists...

2006-05-09 Thread James Stroud
Ángel Gutiérrez Rodríguez wrote: > I would like to have a list of lists N times deep, and my solution is (in > pseudocode): > > def deep(x): > a=[x] > return a > > mylist=[] > for N: mylist=deep(mylist) > > Is there a more elegant way to do it? > > The maine idea is: from a list having the

Re: List of lists of lists of lists...

2006-05-09 Thread bruno at modulix
Ángel Gutiérrez Rodríguez wrote: > I would like to have a list of lists N times deep, and my solution is (in > pseudocode): > > def deep(x): > a=[x] > return a Hint : what's exactly the difference between deep(x) and [x] ? > mylist=[] > for N: mylist=deep(mylist) > > Is there a more elegant