Re: list comprehension question

2012-10-17 Thread Oscar Benjamin
On 17 October 2012 06:09, Dwight Hutto wrote: > On Wed, Oct 17, 2012 at 12:43 AM, Kevin Anthony > wrote: >> Is it not true that list comprehension is much faster the the for loops? >> >> If it is not the correct way of doing this, i appoligize. >> Like i said, I'm learing list comprehension. >> >

Re: list comprehension question

2012-10-17 Thread Terry Reedy
On 10/17/2012 3:13 AM, rusi wrote: On Oct 17, 10:22 am, Terry Reedy wrote: On 10/16/2012 9:54 PM, Kevin Anthony wrote: I've been teaching myself list comprehension, and i've run across something i'm not able to convert. My response is to the part Kevin could *not* convert, not the parts he

Re: list comprehension question

2012-10-17 Thread 88888 Dihedral
rusi於 2012年10月17日星期三UTC+8下午10時50分11秒寫道: > On Oct 17, 7:37 pm, Dave Angel wrote: > > > > > And I'd wager all the improvement is in the inner loop, the dot() function. > > > > Sorry -- red herring! > > > > Changing > > > > def mm1(a,b): return [[sum(x*y for x,y in zip(ra,rb)) for rb in >

Re: list comprehension question

2012-10-17 Thread rusi
On Oct 17, 7:37 pm, Dave Angel wrote: > And I'd wager all the improvement is in the inner loop, the dot() function. Sorry -- red herring! Changing def mm1(a,b): return [[sum(x*y for x,y in zip(ra,rb)) for rb in zip(*b)] for ra in a] to def mm1(a,b): return [[sum([x*y for x,y in zip(ra,rb)])

Re: list comprehension question

2012-10-17 Thread 88888 Dihedral
Dave Angel於 2012年10月17日星期三UTC+8下午10時37分01秒寫道: > On 10/17/2012 10:06 AM, rusi wrote: > > > On Oct 17, 5:33 pm, Dave Angel wrote: > > >> On 10/17/2012 12:43 AM, Kevin Anthony wrote:> Is it not true that list > >> comprehension is much faster the the for loops? > > >> > > >>> If it is not the co

Re: list comprehension question

2012-10-17 Thread Dave Angel
On 10/17/2012 10:06 AM, rusi wrote: > On Oct 17, 5:33 pm, Dave Angel wrote: >> On 10/17/2012 12:43 AM, Kevin Anthony wrote:> Is it not true that list >> comprehension is much faster the the for loops? >> >>> If it is not the correct way of doing this, i appoligize. >>> Like i said, I'm learing li

Re: list comprehension question

2012-10-17 Thread rusi
On Oct 17, 7:06 pm, rusi wrote: > On Oct 17, 5:33 pm, Dave Angel wrote: > > > On 10/17/2012 12:43 AM, Kevin Anthony wrote:> Is it not true that list > > comprehension is much faster the the for loops? > > > > If it is not the correct way of doing this, i appoligize. > > > Like i said, I'm learin

Re: list comprehension question

2012-10-17 Thread rusi
On Oct 17, 5:33 pm, Dave Angel wrote: > On 10/17/2012 12:43 AM, Kevin Anthony wrote:> Is it not true that list > comprehension is much faster the the for loops? > > > If it is not the correct way of doing this, i appoligize. > > Like i said, I'm learing list comprehension. > list comprehensions

Re: list comprehension question

2012-10-17 Thread Dave Angel
On 10/17/2012 12:43 AM, Kevin Anthony wrote: > Is it not true that list comprehension is much faster the the for loops? > > If it is not the correct way of doing this, i appoligize. > Like i said, I'm learing list comprehension. > (Please don't top-post; it ruins the ordering. In these forums,

Re: list comprehension question

2012-10-17 Thread Hans Mulder
On 17/10/12 09:13:57, rusi wrote: > On Oct 17, 10:22 am, Terry Reedy wrote: >> On 10/16/2012 9:54 PM, Kevin Anthony wrote: >> >>> I've been teaching myself list comprehension, and i've run across >>> something i'm not able to convert. >> >> list comprehensions specifically abbreviate the code that

Re: list comprehension question

2012-10-17 Thread rusi
On Oct 17, 10:22 am, Terry Reedy wrote: > On 10/16/2012 9:54 PM, Kevin Anthony wrote: > > > I've been teaching myself list comprehension, and i've run across > > something i'm not able to convert. > > list comprehensions specifically abbreviate the code that they are > (essentially) equivalent to.

Re: list comprehension question

2012-10-16 Thread Terry Reedy
On 10/16/2012 9:54 PM, Kevin Anthony wrote: I've been teaching myself list comprehension, and i've run across something i'm not able to convert. list comprehensions specifically abbreviate the code that they are (essentially) equivalent to. res = [] for item in source: res.append(f(item))

Re: list comprehension question

2012-10-16 Thread Dwight Hutto
On Wed, Oct 17, 2012 at 12:43 AM, Kevin Anthony wrote: > Is it not true that list comprehension is much faster the the for loops? > > If it is not the correct way of doing this, i appoligize. > Like i said, I'm learing list comprehension. > I thought it was matrix multiplication mixed with list co

Re: list comprehension question

2012-10-16 Thread Kevin Anthony
Is it not true that list comprehension is much faster the the for loops? If it is not the correct way of doing this, i appoligize. Like i said, I'm learing list comprehension. Thanks Kevin On Oct 16, 2012 10:14 PM, "Dave Angel" wrote: > On 10/16/2012 09:54 PM, Kevin Anthony wrote: > > I've been

Re: list comprehension question

2012-10-16 Thread rusi
On Oct 17, 7:14 am, Dave Angel wrote: > On 10/16/2012 09:54 PM, Kevin Anthony wrote: > > > > > > > > > > > I've been teaching myself list comprehension, and i've run across something > > i'm not able to convert. > > > here's the original code for matrix multiplcation > > > retmatrix = Matrix(self.

Re: list comprehension question

2012-10-16 Thread Dwight Hutto
On Tue, Oct 16, 2012 at 10:13 PM, Dave Angel wrote: > On 10/16/2012 09:54 PM, Kevin Anthony wrote: >> I've been teaching myself list comprehension, and i've run across something >> i'm not able to convert. >> >> here's the original code for matrix multiplcation >> >> retmatrix = Matrix(self.__row,

Re: list comprehension question

2012-10-16 Thread Dwight Hutto
On Tue, Oct 16, 2012 at 10:13 PM, Dwight Hutto wrote: > On Tue, Oct 16, 2012 at 9:54 PM, Kevin Anthony > wrote: >> I've been teaching myself list comprehension, and i've run across something >> i'm not able to convert. >> >> here's the original code for matrix multiplcation >> >> retmatrix = Matr

Re: list comprehension question

2012-10-16 Thread Dave Angel
On 10/16/2012 09:54 PM, Kevin Anthony wrote: > I've been teaching myself list comprehension, and i've run across something > i'm not able to convert. > > here's the original code for matrix multiplcation > > retmatrix = Matrix(self.__row,other.__col) > for m in range(0,retmatrix.__row): > for n

list comprehension question

2012-10-16 Thread Kevin Anthony
I've been teaching myself list comprehension, and i've run across something i'm not able to convert. here's the original code for matrix multiplcation retmatrix = Matrix(self.__row,other.__col) for m in range(0,retmatrix.__row): for n in range(0,retmatrix.__col): product = 0 f

Re: list comprehension question

2012-02-29 Thread Terry Reedy
On 2/29/2012 8:52 AM, Johann Spies wrote: Please post plain text, the standard for all python.org mailing lists and corresponding newsgroups, and not html. Some readers print the html as plain text, which is confusing and obnoxious. Other like mine, do skip the plain text version and print the

Re: list comprehension question

2012-02-29 Thread Chris Rebert
On Wed, Feb 29, 2012 at 5:52 AM, Johann Spies wrote: > I understand the following: > > In [79]: instansie > instansie > Out[79]: 'Mangosuthu Technikon' > > In [80]: t = [x.alt_name for x in lys] > t = [x.alt_name for x in lys] > > In [81]: t > t > Out[81]: [] > > In [82]: t.append(instansie) > t.a

Re: list comprehension question

2012-02-29 Thread John Gordon
In James Broadhead writes: > On 29 February 2012 13:52, Johann Spies wrote: > > In [82]: t.append(instansie) > > t.append(instansie) > > > > In [83]: t > > t > > Out[83]: ['Mangosuthu Technikon'] > > In [84]: t = [x.alt_name for x in lys].append(instansie) > > t = [x.alt_name for x in lys].ap

Re: list comprehension question

2012-02-29 Thread James Broadhead
On 29 February 2012 13:52, Johann Spies wrote: > In [82]: t.append(instansie) > t.append(instansie) > > In [83]: t > t > Out[83]: ['Mangosuthu Technikon'] > In [84]: t = [x.alt_name for x in lys].append(instansie) > t = [x.alt_name for x in lys].append(instansie) > > In [85]: t > t > > In [86]: t

list comprehension question

2012-02-29 Thread Johann Spies
I understand the following: In [79]: instansie instansie Out[79]: 'Mangosuthu Technikon' In [80]: t = [x.alt_name for x in lys] t = [x.alt_name for x in lys] In [81]: t t Out[81]: [] In [82]: t.append(instansie) t.append(instansie) In [83]: t t Out[83]: ['Mangosuthu Technikon'] But then why d

Re: list comprehension question

2009-05-11 Thread J Kenneth King
Steven D'Aprano writes: > On Thu, 07 May 2009 13:28:10 -0400, J Kenneth King wrote: > >> Steven D'Aprano writes: >> >>> On Wed, 06 May 2009 09:48:51 -0400, J Kenneth King wrote: >>> Emile van Sebille writes: > On 5/5/2009 9:15 AM J Kenneth King said... > >> List comprehe

Re: list comprehension question

2009-05-08 Thread Steven D'Aprano
On Thu, 07 May 2009 13:28:10 -0400, J Kenneth King wrote: > Steven D'Aprano writes: > >> On Wed, 06 May 2009 09:48:51 -0400, J Kenneth King wrote: >> >>> Emile van Sebille writes: >>> On 5/5/2009 9:15 AM J Kenneth King said... > List comprehensions can make a reader of your code

Re: list comprehension question

2009-05-08 Thread J Kenneth King
Terry Reedy writes: > J Kenneth King wrote: >> >> Keep in mind that nested comprehensions are still available because >> they do have a use case that justifies their existence. > > Nested comprehensions are available because because the syntax makes > them available by default and making a fiddly

Re: list comprehension question

2009-05-07 Thread Terry Reedy
J Kenneth King wrote: Keep in mind that nested comprehensions are still available because they do have a use case that justifies their existence. Nested comprehensions are available because because the syntax makes them available by default and making a fiddly exception would be contrary to

Re: list comprehension question

2009-05-07 Thread J Kenneth King
Steven D'Aprano writes: > On Wed, 06 May 2009 09:48:51 -0400, J Kenneth King wrote: > >> Emile van Sebille writes: >> >>> On 5/5/2009 9:15 AM J Kenneth King said... >>> List comprehensions can make a reader of your code apprehensive because it can read like a run-on sentence and thus

Re: list comprehension question

2009-05-07 Thread Lie Ryan
Scott David Daniels wrote: John Posner wrote: Shane Geiger wrote: if type(el) == list or type(el) is tuple: A tiny improvement: if type(el) in (list, tuple): or (even better) if isinstance(el, (list, tuple)) However, it is my contention that you shouldn't be flattening by typ

Re: list comprehension question

2009-05-07 Thread Lie Ryan
Lie Ryan wrote: Steven D'Aprano wrote: "If you’ve got the stomach for it, list comprehensions can be nested. They are a powerful tool but – like all powerful tools – they need to be used carefully, if at all." How does this discourage the use of list comprehensions? At most, it warns that com

Re: list comprehension question

2009-05-07 Thread Lie Ryan
Steven D'Aprano wrote: "If you’ve got the stomach for it, list comprehensions can be nested. They are a powerful tool but – like all powerful tools – they need to be used carefully, if at all." How does this discourage the use of list comprehensions? At most, it warns that complicated list com

Re: list comprehension question

2009-05-06 Thread Steven D'Aprano
On Wed, 06 May 2009 09:48:51 -0400, J Kenneth King wrote: > Emile van Sebille writes: > >> On 5/5/2009 9:15 AM J Kenneth King said... >> >>> List comprehensions can make a reader of your code apprehensive >>> because it can read like a run-on sentence and thus be difficult to >>> parse. The Pyth

Re: list comprehension question

2009-05-06 Thread Emile van Sebille
On 5/6/2009 6:48 AM J Kenneth King said... Emile van Sebille writes: On 5/5/2009 9:15 AM J Kenneth King said... The Python documentation discourages their use and I believe for good reason. Can you provide a link for this? I'd like to see specifically what's being discouraged, as I'd be sur

Re: list comprehension question

2009-05-06 Thread Roel Schroeven
John Yeung schreef: > Essentially, if you see [B A] over and over and over again, when > finally confronted with LCs of more "elements", it's not immediately > clear that the pattern of increasing nestedness is > > [B A] => [C A B] => [D A B C] => etc. > > rather than > > [B A] => [C B A] =>

Re: list comprehension question

2009-05-06 Thread J Kenneth King
Emile van Sebille writes: > On 5/5/2009 9:15 AM J Kenneth King said... > >> List comprehensions can make a reader of your code apprehensive >> because it can read like a run-on sentence and thus be difficult to >> parse. The Python documentation discourages their use and I believe >> for good rea

Re: list comprehension question

2009-05-06 Thread John Yeung
On May 5, 11:36 pm, alex23 wrote: > Apart from the presence of 'item' at the beginning of the > list comprehension as opposed to 'b.append(item)' at the > end of the for-loop, how exactly does the listcomp force > you to "bounce [..] back and forth" to follow the logic? It's precisely the fact t

Re: list comprehension question

2009-05-06 Thread Boris Borcic
Ross wrote: If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to return a new list of each individual element in these tuples, I can do it with a nested for loop but when I try to do it using the list comprehension b = [j for j in i for i in a], my output is b = [5,5,5,6,6,6] inste

Re: list comprehension question

2009-05-06 Thread alex23
On May 6, 2:10 pm, Steven D'Aprano wrote: > It's precisely the indentation and colons (plus newlines) that makes > nested for-loops easier to read than list-comps with multiple fors. > > You can get back *nearly* all the readability by splitting the list comp > into multiple lines: It was less th

Re: list comprehension question

2009-05-05 Thread Terry Reedy
Steven D'Aprano wrote: On Tue, 05 May 2009 13:43:32 -0400, J. Cliff Dyer wrote: Except for the ones that it doesn't cover, like strings: hasattr('abcd', '__iter__') False True in Python3 -- http://mail.python.org/mailman/listinfo/python-list

Re: list comprehension question

2009-05-05 Thread Steven D'Aprano
On Tue, 05 May 2009 20:36:37 -0700, alex23 wrote: > On May 6, 4:01 am, "J. Cliff Dyer" wrote: >> The way you have to bounce your eyes back and forth in the >> comprehension makes it hard to read the logic.  With the loop, on the >> other hand it is blatantly obvious which way the nesting occurs.

Re: list comprehension question

2009-05-05 Thread alex23
On May 6, 4:01 am, "J. Cliff Dyer" wrote: > The way you have to bounce your eyes back and forth in the comprehension > makes it hard to read the logic.  With the loop, on the other hand it is > blatantly obvious which way the nesting occurs. > > [ item for j in a if len(j)==2 for item in j i

Re: yet another list comprehension question

2009-05-05 Thread namekuseijin
2009/5/5 Ricardo Aráoz : > This seems to work for any length tuples : > a = [(1,2), (3,4, 'goes'), (5,None), (6,7, 8, 'as', None), (8, None), (9, 0)] [tup for tup in a if not [e for e in tup if e == None]] > [(1, 2), (3, 4, 'goes'), (9, 0)] Why that extra "for"? KISS >>> a = [(1,2

Re: list comprehension question

2009-05-05 Thread Steven D'Aprano
On Tue, 05 May 2009 13:43:32 -0400, J. Cliff Dyer wrote: > On Fri, 2009-05-01 at 13:00 -0400, John Posner wrote: >> Shane Geiger wrote: >> >if type(el) == list or type(el) is tuple: >> A tiny improvement: >> >> if type(el) in (list, tuple): >> >> > Another alternative, which might b

Re: yet another list comprehension question

2009-05-05 Thread Ricardo Aráoz
namekuseijin wrote: > On May 4, 9:15 am, David Robinow wrote: > >> On Mon, May 4, 2009 at 2:33 AM, namekuseijin >> >> wrote: >> >> ls = [(1,2), (3,4), (5, None), (6,7), (8, None)] >> [(x,y) for (x,y) in ls if y] >> >>> [(1, 2), (3, 4), (6, 7)] >>> >> Nope.

Re: list comprehension question

2009-05-05 Thread J. Cliff Dyer
On Tue, 2009-05-05 at 12:15 -0400, J Kenneth King wrote: > Emile van Sebille writes: > > > On 5/1/2009 7:31 AM J Kenneth King said... > >> Chris Rebert writes: > >>> b = [] > >>> for pair in a: > >>> for item in pair: > >>> b.append(item) > >> > >> This is much more clear than a nest

Re: Re: list comprehension question

2009-05-05 Thread J. Cliff Dyer
On Fri, 2009-05-01 at 13:00 -0400, John Posner wrote: > Shane Geiger wrote: > >if type(el) == list or type(el) is tuple: > A tiny improvement: > > if type(el) in (list, tuple): > Another alternative, which might be useful in some cases: if hasattr(el, '__iter__'): This co

Re: list comprehension question

2009-05-05 Thread Emile van Sebille
On 5/5/2009 9:15 AM J Kenneth King said... List comprehensions can make a reader of your code apprehensive because it can read like a run-on sentence and thus be difficult to parse. The Python documentation discourages their use and I believe for good reason. Can you provide a link for this?

Re: list comprehension question

2009-05-05 Thread J Kenneth King
Emile van Sebille writes: > On 5/1/2009 7:31 AM J Kenneth King said... >> Chris Rebert writes: >>> b = [] >>> for pair in a: >>> for item in pair: >>> b.append(item) >> >> This is much more clear than a nested comprehension. >> >> I love comprehensions, but abusing them can lead to r

Re: yet another list comprehension question

2009-05-04 Thread namekuseijin
On May 4, 9:15 am, David Robinow wrote: > On Mon, May 4, 2009 at 2:33 AM, namekuseijin > > wrote: > ls = [(1,2), (3,4), (5, None), (6,7), (8, None)] > [(x,y) for (x,y) in ls if y] > > [(1, 2), (3, 4), (6, 7)] > > Nope. That filters out 0 as well as None. Not what the OP asked for. True

Re: yet another list comprehension question

2009-05-04 Thread David Robinow
On Mon, May 4, 2009 at 2:33 AM, namekuseijin wrote: ls = [(1,2), (3,4), (5, None), (6,7), (8, None)] [(x,y) for (x,y) in ls if y] > [(1, 2), (3, 4), (6, 7)] Nope. That filters out 0 as well as None. Not what the OP asked for. -- http://mail.python.org/mailman/listinfo/python-list

Re: yet another list comprehension question

2009-05-04 Thread Arnaud Delobelle
Snorri H writes: > On May 3, 6:13 am, Ross wrote: >> I'm trying to set up a simple filter using a list comprehension. If I >> have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)] >> and I wanted to filter out all tuples containing None, I would like to >> get the new list b = [(

Re: yet another list comprehension question

2009-05-04 Thread Snorri H
On May 3, 6:13 am, Ross wrote: > I'm trying to set up a simple filter using a list comprehension. If I > have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)] > and I wanted to filter out all tuples containing None, I would like to > get the new list b = [(1,2), (3,4),(6,7)]. > > I

Re: yet another list comprehension question

2009-05-03 Thread namekuseijin
>>> ls = [(1,2), (3,4), (5, None), (6,7), (8, None)] >>> [(x,y) for (x,y) in ls if y] [(1, 2), (3, 4), (6, 7)] -- http://mail.python.org/mailman/listinfo/python-list

Re: yet another list comprehension question

2009-05-03 Thread ma
This isn't list comprehension, but it's something to keep in mind: b = filter(lambda x: None not in x, input_list) On Sat, May 2, 2009 at 10:25 PM, CTO wrote: > On May 2, 10:13 pm, Ross wrote: > > I'm trying to set up a simple filter using a list comprehension. If I > > have a list of tuples,

Re: yet another list comprehension question

2009-05-02 Thread CTO
On May 2, 10:13 pm, Ross wrote: > I'm trying to set up a simple filter using a list comprehension. If I > have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)] > and I wanted to filter out all tuples containing None, I would like to > get the new list b = [(1,2), (3,4),(6,7)]. try

Re: yet another list comprehension question

2009-05-02 Thread Ross
On May 2, 7:21 pm, Chris Rebert wrote: > On Sat, May 2, 2009 at 7:13 PM, Ross wrote: > > I'm trying to set up a simple filter using a list comprehension. If I > > have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)] > > and I wanted to filter out all tuples containing None, I wou

Re: yet another list comprehension question

2009-05-02 Thread Chris Rebert
On Sat, May 2, 2009 at 7:13 PM, Ross wrote: > I'm trying to set up a simple filter using a list comprehension. If I > have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)] > and I wanted to filter out all tuples containing None, I would like to > get the new list b = [(1,2), (3,4),

yet another list comprehension question

2009-05-02 Thread Ross
I'm trying to set up a simple filter using a list comprehension. If I have a list of tuples, a = [(1,2), (3,4), (5,None), (6,7), (8, None)] and I wanted to filter out all tuples containing None, I would like to get the new list b = [(1,2), (3,4),(6,7)]. I tried b = [i for i in a if t for t in i is

Re: list comprehension question

2009-05-01 Thread Scott David Daniels
John Posner wrote: Shane Geiger wrote: if type(el) == list or type(el) is tuple: A tiny improvement: if type(el) in (list, tuple): or (even better) if isinstance(el, (list, tuple)) However, it is my contention that you shouldn't be flattening by type -- you should know where,

Re: list comprehension question

2009-05-01 Thread Arnaud Delobelle
Emile van Sebille writes: > On 5/1/2009 9:19 AM Arnaud Delobelle said... >> Emile van Sebille writes: >>> On 5/1/2009 7:31 AM J Kenneth King said... I love comprehensions, but abusing them can lead to really dense and difficult to read code. >>> I disagree on dense and difficult, altho

Re: list comprehension question

2009-05-01 Thread Emile van Sebille
On 5/1/2009 9:19 AM Arnaud Delobelle said... Emile van Sebille writes: On 5/1/2009 7:31 AM J Kenneth King said... I love comprehensions, but abusing them can lead to really dense and difficult to read code. I disagree on dense and difficult, although I'll leave open the question of abuse. b

Re: Re: list comprehension question

2009-05-01 Thread John Posner
Shane Geiger wrote: if type(el) == list or type(el) is tuple: A tiny improvement: if type(el) in (list, tuple): -- http://mail.python.org/mailman/listinfo/python-list

Re: list comprehension question

2009-05-01 Thread Shane Geiger
from goopy.functional import flatten # http://sourceforge.net/projects/goog-goopy/ b = [(1,2), (3,4), (5,6)] print flatten(b) #from goopy.functional import flatten # http://sourceforge.net/projects/goog-goopy/ def flatten(seq): """ Returns a list of the contents of seq with sublists and

Re: list comprehension question

2009-05-01 Thread Arnaud Delobelle
Emile van Sebille writes: > On 5/1/2009 7:31 AM J Kenneth King said... >> Chris Rebert writes: >>> b = [] >>> for pair in a: >>> for item in pair: >>> b.append(item) >> >> This is much more clear than a nested comprehension. >> >> I love comprehensions, but abusing them can lead to r

Re: list comprehension question

2009-05-01 Thread Emile van Sebille
On 5/1/2009 7:31 AM J Kenneth King said... Chris Rebert writes: b = [] for pair in a: for item in pair: b.append(item) This is much more clear than a nested comprehension. I love comprehensions, but abusing them can lead to really dense and difficult to read code. I disagree on

Re: list comprehension question

2009-05-01 Thread J Kenneth King
Chris Rebert writes: > On Thu, Apr 30, 2009 at 5:56 PM, Ross wrote: >> If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to >> return a new list of each individual element in these tuples, I can do >> it with a nested for loop but when I try to do it using the list >> comprehensio

Re: list comprehension question

2009-05-01 Thread thor
On May 1, 2:28 pm, Arnaud Delobelle wrote: > Ross writes: > > If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to > > return a new list of each individual element in these tuples, I can do > > it with a nested for loop but when I try to do it using the list > > comprehension b = [

Re: list comprehension question

2009-04-30 Thread Arnaud Delobelle
Ross writes: > If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to > return a new list of each individual element in these tuples, I can do > it with a nested for loop but when I try to do it using the list > comprehension b = [j for j in i for i in a], my output is b = > [5,5,5,6

Re: list comprehension question

2009-04-30 Thread Michael Spencer
Ross wrote: If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to return a new list of each individual element in these tuples, I can do it with a nested for loop but when I try to do it using the list comprehension b = [j for j in i for i in a], my output is b = [5,5,5,6,6,6] inste

Re: list comprehension question

2009-04-30 Thread Chris Rebert
On Thu, Apr 30, 2009 at 5:56 PM, Ross wrote: > If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to > return a new list of each individual element in these tuples, I can do > it with a nested for loop but when I try to do it using the list > comprehension b = [j for j in i for i in

list comprehension question

2009-04-30 Thread Ross
If I have a list of tuples a = [(1,2), (3,4), (5,6)], and I want to return a new list of each individual element in these tuples, I can do it with a nested for loop but when I try to do it using the list comprehension b = [j for j in i for i in a], my output is b = [5,5,5,6,6,6] instead of the corr

Re: quick beginners List comprehension question

2009-01-21 Thread Srinivasa NL
You can try this import random class foo: def __init__(self): self.bar = random.randint(1,100) def getbar(ls,i): ls.append(foo()) ls[i].bar = ls[i].bar * 3 ls = [] [getbar(ls,i) for i in range(10)] On Thu, Jan 22, 2009 at 4:45 AM, Diez B. Roggisch wrote: > MRAB schrieb: > >> D

Re: quick beginners List comprehension question

2009-01-21 Thread Diez B. Roggisch
MRAB schrieb: Diez B. Roggisch wrote: Dr Mephesto wrote: Hi, Im new to python, and OOP, and am trying to get a handle on list comprehension. Say I have a class Foo with a property called bar: class Foo: def __init__(self): self.bar = random.randint(1,100) and then I make a list

Re: quick beginners List comprehension question

2009-01-21 Thread Terry Reedy
Dr Mephesto wrote: Hi, Im new to python, and OOP, and am trying to get a handle on list comprehension. Say I have a class Foo with a property called bar: class Foo: def __init__(self): self.bar = random.randint(1,100) and then I make a list of these objects: Newlist = [] for x in

Re: quick beginners List comprehension question

2009-01-21 Thread Philip Semanchuk
On Jan 21, 2009, at 11:52 AM, Lou Pecora wrote: In article , Philip Semanchuk wrote: Other answers have been good; to them I'll add the comment that list comprehensions are for *constructing* lists, not manipulating the elements thereof. HTH Philip Well this seems to work just fine. Wh

Re: quick beginners List comprehension question

2009-01-21 Thread Steve Holden
Lou Pecora wrote: > In article , > Philip Semanchuk wrote: > >> Other answers have been good; to them I'll add the comment that list >> comprehensions are for *constructing* lists, not manipulating the >> elements thereof. >> >> HTH >> Philip > > > Well this seems to work just fine. What

Re: quick beginners List comprehension question

2009-01-21 Thread Lou Pecora
In article , Philip Semanchuk wrote: > > Other answers have been good; to them I'll add the comment that list > comprehensions are for *constructing* lists, not manipulating the > elements thereof. > > HTH > Philip Well this seems to work just fine. What am I missing: A=[1,2,3] p

Re: quick beginners List comprehension question

2009-01-21 Thread MRAB
Diez B. Roggisch wrote: Dr Mephesto wrote: Hi, Im new to python, and OOP, and am trying to get a handle on list comprehension. Say I have a class Foo with a property called bar: class Foo: def __init__(self): self.bar = random.randint(1,100) and then I make a list of these object

Re: quick beginners List comprehension question

2009-01-21 Thread Philip Semanchuk
On Jan 21, 2009, at 10:52 AM, Dr Mephesto wrote: Hi, Im new to python, and OOP, and am trying to get a handle on list comprehension. Say I have a class Foo with a property called bar: class Foo: def __init__(self): self.bar = random.randint(1,100) and then I make a list of these ob

Re: quick beginners List comprehension question

2009-01-21 Thread MRAB
Dr Mephesto wrote: Hi, Im new to python, and OOP, and am trying to get a handle on list comprehension. Say I have a class Foo with a property called bar: class Foo: def __init__(self): self.bar = random.randint(1,100) and then I make a list of these objects: Newlist = [] for x in

Re: quick beginners List comprehension question

2009-01-21 Thread Diez B. Roggisch
Dr Mephesto wrote: > Hi, > Im new to python, and OOP, and am trying to get a handle on list > comprehension. > > Say I have a class Foo with a property called bar: > > class Foo: > def __init__(self): > self.bar = random.randint(1,100) > > and then I make a list of these objects: >

quick beginners List comprehension question

2009-01-21 Thread Dr Mephesto
Hi, Im new to python, and OOP, and am trying to get a handle on list comprehension. Say I have a class Foo with a property called bar: class Foo: def __init__(self): self.bar = random.randint(1,100) and then I make a list of these objects: Newlist = [] for x in range(10): Newlis

Re: List Comprehension Question: One to Many Mapping?

2007-09-14 Thread beginner
On Aug 24, 5:35 am, Boris Borcic <[EMAIL PROTECTED]> wrote: > Paul Rubin wrote: > > beginner <[EMAIL PROTECTED]> writes: > >> For example, if I have x=[ [1,2], [3,4] ] > > >> What I want is a new list of list that has four sub-lists: > > >> [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] > > > [[a, map(

Re: List Comprehension Question: One to Many Mapping?

2007-08-24 Thread Eduardo O. Padoan
> For example, if I have x=[ [1,2], [3,4] ] > > What I want is a new list of list that has four sub-lists: > > [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] [[a, [f(b) for b in a]] for a in x] -- http://mail.python.org/mailman/listinfo/python-list

Re: List Comprehension Question: One to Many Mapping?

2007-08-24 Thread ZeD
Boris Borcic wrote: >>> For example, if I have x=[ [1,2], [3,4] ] >>> What I want is a new list of list that has four sub-lists: >>> [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] >> [[a, map(f,a)] for a in x] > [map(g,a) for a in x for g in [None,f]] > will do it. > > ...a bit too cleverly, but ther

Re: List Comprehension Question: One to Many Mapping?

2007-08-24 Thread beginner
On Aug 24, 5:47 am, Paul Rubin wrote: > Boris Borcic <[EMAIL PROTECTED]> writes: > > >> For example, if I have x=[ [1,2], [3,4] ] > > > >> What I want is a new list of list that has four sub-lists: > > > >> [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] > > > [[a, map(f,a)] f

Re: List Comprehension Question: One to Many Mapping?

2007-08-24 Thread beginner
On Aug 24, 12:44 am, beginner <[EMAIL PROTECTED]> wrote: > On Aug 24, 12:41 am, Davo <[EMAIL PROTECTED]> wrote: > > > > > > > On Aug 23, 9:24 pm, beginner <[EMAIL PROTECTED]> wrote: > > > > Hi All, > > > > How do I map a list to two lists with list comprehension? > > > > For example, if I have x=[

Re: List Comprehension Question: One to Many Mapping?

2007-08-24 Thread Paul Rubin
Boris Borcic <[EMAIL PROTECTED]> writes: > >> For example, if I have x=[ [1,2], [3,4] ] > >> > >> What I want is a new list of list that has four sub-lists: > >> > >> [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] > > [[a, map(f,a)] for a in x] > > no, that one will be [[[1,2], [f(1), f(2)]], [[3,4],

Re: List Comprehension Question: One to Many Mapping?

2007-08-24 Thread Boris Borcic
Paul Rubin wrote: > beginner <[EMAIL PROTECTED]> writes: >> For example, if I have x=[ [1,2], [3,4] ] >> >> What I want is a new list of list that has four sub-lists: >> >> [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] > > [[a, map(f,a)] for a in x] no, that one will be [[[1,2], [f(1), f(2)]], [[3,4

Re: List Comprehension Question: One to Many Mapping?

2007-08-23 Thread Paul Rubin
beginner <[EMAIL PROTECTED]> writes: > For example, if I have x=[ [1,2], [3,4] ] > > What I want is a new list of list that has four sub-lists: > > [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] [[a, map(f,a)] for a in x] -- http://mail.python.org/mailman/listinfo/python-list

Re: List Comprehension Question: One to Many Mapping?

2007-08-23 Thread beginner
On Aug 24, 12:41 am, Davo <[EMAIL PROTECTED]> wrote: > On Aug 23, 9:24 pm, beginner <[EMAIL PROTECTED]> wrote: > > > > > > > Hi All, > > > How do I map a list to two lists with list comprehension? > > > For example, if I have x=[ [1,2], [3,4] ] > > > What I want is a new list of list that has four

Re: List Comprehension Question: One to Many Mapping?

2007-08-23 Thread Davo
On Aug 23, 9:24 pm, beginner <[EMAIL PROTECTED]> wrote: > Hi All, > > How do I map a list to two lists with list comprehension? > > For example, if I have x=[ [1,2], [3,4] ] > > What I want is a new list of list that has four sub-lists: > > [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] > > [1,2] is ma

Re: List Comprehension Question: One to Many Mapping?

2007-08-23 Thread Peter Otten
beginner wrote: > How do I map a list to two lists with list comprehension? > > For example, if I have x=[ [1,2], [3,4] ] > > What I want is a new list of list that has four sub-lists: > > [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] > > [1,2] is mapped to [1,2] and [f(1), f(2)] and [3,4] is map

Re: List Comprehension Question: One to Many Mapping?

2007-08-23 Thread [EMAIL PROTECTED]
On Aug 23, 9:24 pm, beginner <[EMAIL PROTECTED]> wrote: > Hi All, > > How do I map a list to two lists with list comprehension? > > For example, if I have x=[ [1,2], [3,4] ] > > What I want is a new list of list that has four sub-lists: > > [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] > > [1,2] is ma

List Comprehension Question: One to Many Mapping?

2007-08-23 Thread beginner
Hi All, How do I map a list to two lists with list comprehension? For example, if I have x=[ [1,2], [3,4] ] What I want is a new list of list that has four sub-lists: [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]] [1,2] is mapped to [1,2] and [f(1), f(2)] and [3,4] is mapped to [3,4], [f(3), f(4)]