Re: [Tutor] reduce with comprehension

2005-11-24 Thread Alan Gauld
> But in the general case can you do the same job as > reduce in a list comprehension? >>> def f(x, y): ... return x + y ... >>> tmp = [0] >>> [f(x, y) for x in arr for y in [tmp[-1]] if tmp.append(f(x, y)) or >>> True][-1] 45 Let's try some more... >>> def f(x, y): ... return x*y ... >>> tmp

Re: [Tutor] reduce with comprehension

2005-11-24 Thread John Fouhy
On 25/11/05, Nick Lunt <[EMAIL PROTECTED]> wrote: > > -Original Message- > > >>> def f(x, y): > > ... return x*y > > ... > > >>> arr = range(1, 10)# Don't want to include 0! > > >>> reduce(f, arr) # Our target > > 362880 > > >>> tmp = [1] > > >>> [f(x, y) for x in arr for y i

Re: [Tutor] reduce with comprehension

2005-11-24 Thread Nick Lunt
> -Original Message- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] > >>> def f(x, y): > ... return x + y > ... > >>> arr = range(10) > >>> sum(arr) # Our target > 45 > >>> tmp = [0] > >>> [f(x, y) for x in arr for y in [tmp[-1]] if tmp.append(f(x, > y)) or True][-1] >

Re: [Tutor] reduce with comprehension

2005-11-24 Thread John Fouhy
On 22/11/05, Alan Gauld <[EMAIL PROTECTED]> wrote: > But in the general case can you do the same job as > reduce in a list comprehension? ie apply an operation > to the first two elements of a sequence and replace > them with the result, then repeat until the list becomes > a single value? Well ..

Re: [Tutor] reduce with comprehension

2005-11-24 Thread Alan Gauld
Hi John, > Everything is possible with list comprehensions! >>> [x for z in a for y in z for x in y] > [1, 2, 3, 31, 32, 4, 5, 6, 7, 71, 72, 8, 9] impressive! But in the general case can you do the same job as reduce in a list comprehension? ie apply an operation to the first two elements of

Re: [Tutor] reduce with comprehension

2005-11-21 Thread János Juhász
Hi John, thanks it. It is great. I looked for it, but I couldn't made it. I have tried it with wrong order: # I have tried it so [x for x in y for y in a] [[8], [8], [8], [9], [9], [9]] # that is wrong, # Instead of that you wrote [x for y in a for x in y] [[1], [2], [3, 31, 32], [4], [5], [6],

Re: [Tutor] reduce with comprehension

2005-11-21 Thread John Fouhy
On 21/11/05, János Juhász <[EMAIL PROTECTED]> wrote: > I can't imagine how this could be made with list comprehension. > > >>> import operator > >>> a = (([1],[2],[3,31,32],[4]), ([5],[6],[7, 71, 72]), ([8],[9])) > >>> reduce(operator.add, a) # it makes a long list now > ([1], [2], [3, 31, 32], [4]

Re: [Tutor] reduce with comprehension

2005-11-21 Thread Kent Johnson
János Juhász wrote: > Hi, > > I can't imagine how this could be made with list comprehension. > import operator a = (([1],[2],[3,31,32],[4]), ([5],[6],[7, 71, 72]), ([8],[9])) reduce(operator.add, a) # it makes a long list now > > Is it possible to substitute reduce with comprehensio

Re: [Tutor] reduce with comprehension

2005-11-21 Thread Alan Gauld
>>> a = (([1],[2],[3,31,32],[4]), ([5],[6],[7, 71, 72]), ([8],[9])) >>> reduce(operator.add, a) # it makes a long list now ([1], [2], [3, 31, 32], [4], [5], [6], [7, 71, 72], [8], [9]) When I make list comprehension, the list hierarchy is allways the same or >>> [item for item in a] # the deepnes

Re: [Tutor] reduce with comprehension

2005-11-21 Thread Brian van den Broek
János Juhász said unto the world upon 2005-11-21 01:20: > Hi, > > I can't imagine how this could be made with list comprehension. > > import operator a = (([1],[2],[3,31,32],[4]), ([5],[6],[7, 71, 72]), ([8],[9])) reduce(operator.add, a) # it makes a long list now > > ([1], [2], [3,

[Tutor] reduce with comprehension

2005-11-20 Thread János Juhász
Hi, I can't imagine how this could be made with list comprehension. >>> import operator >>> a = (([1],[2],[3,31,32],[4]), ([5],[6],[7, 71, 72]), ([8],[9])) >>> reduce(operator.add, a) # it makes a long list now ([1], [2], [3, 31, 32], [4], [5], [6], [7, 71, 72], [8], [9]) When I make list compre