Re: [Tutor] Alternative to nested loops

2006-03-19 Thread Steve Nelson
On 3/19/06, Alan Gauld <[EMAIL PROTECTED]> wrote: > > interesting. I've never done any functional programming at all, so it > > all seems a little foreign! > > > > Can you recommend another gentle introduction? > > Try the functional programming topic in the Advanced section of my tutor. > It cov

Re: [Tutor] Alternative to nested loops

2006-03-19 Thread Alan Gauld
> interesting. I've never done any functional programming at all, so it > all seems a little foreign! > > Can you recommend another gentle introduction? Try the functional programming topic in the Advanced section of my tutor. It covers the concepts and how to do it in Python at the basic leve

Re: [Tutor] Alternative to nested loops

2006-03-19 Thread Steve Nelson
On 3/19/06, John Fouhy <[EMAIL PROTECTED]> wrote: > What you're doing is called "flattening" a list. You can do it with a > list comprehension: > > >>> foo = [[1,2,3], [4,5,6], [7,8,9]] > >>> [x for y in foo for x in y] > [1, 2, 3, 4, 5, 6, 7, 8, 9] Ah yes, that was the sort of thing I was think

Re: [Tutor] Alternative to nested loops

2006-03-19 Thread Steve Nelson
On 3/19/06, Karl Pflästerer <[EMAIL PROTECTED]> wrote: >>> reduce(lambda s, L: s + sum(L), foo, 0) Ah ok - well that looks pretty cryptic to me, as I've never used either lambda or reduce(). However, this looks to be a 'functional' way of doing what I was doing procedurally, which is, I suppose,

Re: [Tutor] Alternative to nested loops

2006-03-19 Thread John Fouhy
On 20/03/06, Steve Nelson <[EMAIL PROTECTED]> wrote: > Hi All, > > I had a feeling I could do this: > > >>> foo > [[1, 2, 3], [1, 2, 3], [1, 2, 3]] > >>> for c in foo: > ... for b in c: > ... print b What you're doing is called "flattening" a list. You can do it with a list compre

Re: [Tutor] Alternative to nested loops

2006-03-19 Thread Karl Pflästerer
On 19 Mrz 2006, [EMAIL PROTECTED] wrote: > I had a feeling I could do this: > foo > [[1, 2, 3], [1, 2, 3], [1, 2, 3]] for c in foo: > ... for b in c: > ... print b > ... > 1 > 2 > 3 > 1 > 2 > 3 > 1 > 2 > 3 > > Using a list comprehension, as it seemed to me like I was sayi

[Tutor] Alternative to nested loops

2006-03-19 Thread Steve Nelson
Hi All, I had a feeling I could do this: >>> foo [[1, 2, 3], [1, 2, 3], [1, 2, 3]] >>> for c in foo: ... for b in c: ... print b ... 1 2 3 1 2 3 1 2 3 Using a list comprehension, as it seemed to me like I was saying: b for c in foo, but I can't see how to do this. Ultimately I w