FAQ
Hi all,


I'm a bit stuck with this so maybe someone can help me.


I'm writing a little program that needs to iterate through several nested
'for' loops. However, the number of loops (and therefore the depth of the
nesting) cannot be predetermined.


I've read that lots of nested loops are bad form anyway, and that "you
should do it with functions instead".


However I still can't work out how to set the number dynamically.




for ((l=0; l<10; l++)); do
? ?
  for ((m=0; m<10; m++))
?;?
do


? ?
for (n=0; n<10; n++)); do


? ?
something
? that uses l, m and n?


     done
   done
done


So this is what it needs to look like if nest_depth=3


But how do I write a program that iterates something
? for nest_depth=4 (which in this case would use l, m, n and o)?, or any
other number (which can't be predicted)?


cheers,
Doug


PS I didn't write this in Python just for speed, but feel free to reply in
Python if you want - the practice would be good for me.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edinburgh/attachments/20141118/f34af124/attachment.html>

Search Discussions

  • Tom Dalton at Nov 18, 2014 at 12:53 pm
    Hey Doug,


    There are a few ways you could do this, but it would be helpful if you
    could give a little more context on where you get l,m and n from and where
    the limit (10) comes from for each (or how it's determined). Also, what
    does the thing that uses those variables look like? Is it separate function
    calls, or a function that takes a list of arguments, or something else?


    Cheers,


    Tom


    On 18 November 2014 12:08, Douglas Houston wrote:

    Hi all,

    I'm a bit stuck with this so maybe someone can help me.

    I'm writing a little program that needs to iterate through several nested
    'for' loops. However, the number of loops (and therefore the depth of the
    nesting) cannot be predetermined.

    I've read that lots of nested loops are bad form anyway, and that "you
    should do it with functions instead".

    However I still can't work out how to set the number dynamically.


    for ((l=0; l<10; l++)); do
    ? ?
    for ((m=0; m<10; m++))
    ?;?
    do

    ? ?
    for (n=0; n<10; n++)); do

    ? ?
    something
    ? that uses l, m and n?

    done
    done
    done

    So this is what it needs to look like if nest_depth=3

    But how do I write a program that iterates something
    ? for nest_depth=4 (which in this case would use l, m, n and o)?, or any
    other number (which can't be predicted)?

    cheers,
    Doug

    PS I didn't write this in Python just for speed, but feel free to reply in
    Python if you want - the practice would be good for me.

    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/edinburgh/attachments/20141118/1c32a23a/attachment.html>
  • Kenny Erasmuson at Nov 18, 2014 at 12:56 pm
    It sounds like some kind of recursive function may be needed here. However,
    it also looks like the 'inner' most function call will then be operating on
    a variable number of parameters (e.g. l, m and n in your example, but it
    could be l, m, n, k, z, etc). Is that right?


    On 18 November 2014 12:53, Tom Dalton wrote:

    Hey Doug,

    There are a few ways you could do this, but it would be helpful if you
    could give a little more context on where you get l,m and n from and where
    the limit (10) comes from for each (or how it's determined). Also, what
    does the thing that uses those variables look like? Is it separate function
    calls, or a function that takes a list of arguments, or something else?

    Cheers,

    Tom

    On 18 November 2014 12:08, Douglas Houston <douglasrhouston at googlemail.com
    wrote:
    Hi all,

    I'm a bit stuck with this so maybe someone can help me.

    I'm writing a little program that needs to iterate through several nested
    'for' loops. However, the number of loops (and therefore the depth of the
    nesting) cannot be predetermined.

    I've read that lots of nested loops are bad form anyway, and that "you
    should do it with functions instead".

    However I still can't work out how to set the number dynamically.


    for ((l=0; l<10; l++)); do
    ? ?
    for ((m=0; m<10; m++))
    ?;?
    do

    ? ?
    for (n=0; n<10; n++)); do

    ? ?
    something
    ? that uses l, m and n?

    done
    done
    done

    So this is what it needs to look like if nest_depth=3

    But how do I write a program that iterates something
    ? for nest_depth=4 (which in this case would use l, m, n and o)?, or any
    other number (which can't be predicted)?

    cheers,
    Doug

    PS I didn't write this in Python just for speed, but feel free to reply
    in Python if you want - the practice would be good for me.

    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh
    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/edinburgh/attachments/20141118/88cb4da5/attachment.html>
  • Andre Engelbrecht at Nov 18, 2014 at 1:04 pm
    Something like this maybe?


    https://gist.github.com/andrewebdev/e3a979cfb46380884f3a


    Andre Engelbrecht
    Web Developer/Designer
    http://andre.engelbrechtonline.net
    http://www.tehnode.co.uk


    Too brief? Here's why! http://emailcharter.org


    On Tue, Nov 18, 2014 at 12:56 PM, Kenny Erasmuson wrote:

    It sounds like some kind of recursive function may be needed here.
    However, it also looks like the 'inner' most function call will then be
    operating on a variable number of parameters (e.g. l, m and n in your
    example, but it could be l, m, n, k, z, etc). Is that right?
    On 18 November 2014 12:53, Tom Dalton wrote:

    Hey Doug,

    There are a few ways you could do this, but it would be helpful if you
    could give a little more context on where you get l,m and n from and where
    the limit (10) comes from for each (or how it's determined). Also, what
    does the thing that uses those variables look like? Is it separate function
    calls, or a function that takes a list of arguments, or something else?

    Cheers,

    Tom

    On 18 November 2014 12:08, Douglas Houston <
    douglasrhouston at googlemail.com> wrote:
    Hi all,

    I'm a bit stuck with this so maybe someone can help me.

    I'm writing a little program that needs to iterate through several
    nested 'for' loops. However, the number of loops (and therefore the depth
    of the nesting) cannot be predetermined.

    I've read that lots of nested loops are bad form anyway, and that "you
    should do it with functions instead".

    However I still can't work out how to set the number dynamically.


    for ((l=0; l<10; l++)); do
    ? ?
    for ((m=0; m<10; m++))
    ?;?
    do

    ? ?
    for (n=0; n<10; n++)); do

    ? ?
    something
    ? that uses l, m and n?

    done
    done
    done

    So this is what it needs to look like if nest_depth=3

    But how do I write a program that iterates something
    ? for nest_depth=4 (which in this case would use l, m, n and o)?, or any
    other number (which can't be predicted)?

    cheers,
    Doug

    PS I didn't write this in Python just for speed, but feel free to reply
    in Python if you want - the practice would be good for me.

    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh
    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh
    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/edinburgh/attachments/20141118/937bf1fa/attachment-0001.html>
  • Tom Dalton at Nov 18, 2014 at 1:07 pm
    You can use itertools.product to simplify this even further. If the
    max-count for each loop is the same then you can do:


    import itertools
    N=3
    for x in itertools.product(range(10), repeat=N):
         print x


    If the max-count is different you can specify the range for each part
    separately:


    for x in itertools.product(range(2), range(3), range(4)):
         print x


    Ref https://docs.python.org/2/library/itertools.html#itertools.product


    Tom


    On 18 November 2014 13:04, Andre Engelbrecht wrote:

    Something like this maybe?

    https://gist.github.com/andrewebdev/e3a979cfb46380884f3a

    Andre Engelbrecht
    Web Developer/Designer
    http://andre.engelbrechtonline.net
    http://www.tehnode.co.uk

    Too brief? Here's why! http://emailcharter.org

    On Tue, Nov 18, 2014 at 12:56 PM, Kenny Erasmuson <
    kennyerasmuson at gmail.com> wrote:
    It sounds like some kind of recursive function may be needed here.
    However, it also looks like the 'inner' most function call will then be
    operating on a variable number of parameters (e.g. l, m and n in your
    example, but it could be l, m, n, k, z, etc). Is that right?
    On 18 November 2014 12:53, Tom Dalton wrote:

    Hey Doug,

    There are a few ways you could do this, but it would be helpful if you
    could give a little more context on where you get l,m and n from and where
    the limit (10) comes from for each (or how it's determined). Also, what
    does the thing that uses those variables look like? Is it separate function
    calls, or a function that takes a list of arguments, or something else?

    Cheers,

    Tom

    On 18 November 2014 12:08, Douglas Houston <
    douglasrhouston at googlemail.com> wrote:
    Hi all,

    I'm a bit stuck with this so maybe someone can help me.

    I'm writing a little program that needs to iterate through several
    nested 'for' loops. However, the number of loops (and therefore the depth
    of the nesting) cannot be predetermined.

    I've read that lots of nested loops are bad form anyway, and that "you
    should do it with functions instead".

    However I still can't work out how to set the number dynamically.


    for ((l=0; l<10; l++)); do
    ? ?
    for ((m=0; m<10; m++))
    ?;?
    do

    ? ?
    for (n=0; n<10; n++)); do

    ? ?
    something
    ? that uses l, m and n?

    done
    done
    done

    So this is what it needs to look like if nest_depth=3

    But how do I write a program that iterates something
    ? for nest_depth=4 (which in this case would use l, m, n and o)?, or
    any other number (which can't be predicted)?

    cheers,
    Doug

    PS I didn't write this in Python just for speed, but feel free to reply
    in Python if you want - the practice would be good for me.

    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh
    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh
    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh
    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/edinburgh/attachments/20141118/501a432d/attachment.html>
  • Andre Engelbrecht at Nov 18, 2014 at 1:20 pm
    Actually, Tom's use of itertools is more accurate than my patch-job :)


    Andre Engelbrecht
    Web Developer/Designer
    http://andre.engelbrechtonline.net
    http://www.tehnode.co.uk


    Too brief? Here's why! http://emailcharter.org


    On Tue, Nov 18, 2014 at 1:07 PM, Tom Dalton wrote:

    You can use itertools.product to simplify this even further. If the
    max-count for each loop is the same then you can do:

    import itertools
    N=3
    for x in itertools.product(range(10), repeat=N):
    print x

    If the max-count is different you can specify the range for each part
    separately:

    for x in itertools.product(range(2), range(3), range(4)):
    print x

    Ref https://docs.python.org/2/library/itertools.html#itertools.product

    Tom
    On 18 November 2014 13:04, Andre Engelbrecht wrote:

    Something like this maybe?

    https://gist.github.com/andrewebdev/e3a979cfb46380884f3a

    Andre Engelbrecht
    Web Developer/Designer
    http://andre.engelbrechtonline.net
    http://www.tehnode.co.uk

    Too brief? Here's why! http://emailcharter.org

    On Tue, Nov 18, 2014 at 12:56 PM, Kenny Erasmuson <
    kennyerasmuson at gmail.com> wrote:
    It sounds like some kind of recursive function may be needed here.
    However, it also looks like the 'inner' most function call will then be
    operating on a variable number of parameters (e.g. l, m and n in your
    example, but it could be l, m, n, k, z, etc). Is that right?
    On 18 November 2014 12:53, Tom Dalton wrote:

    Hey Doug,

    There are a few ways you could do this, but it would be helpful if you
    could give a little more context on where you get l,m and n from and where
    the limit (10) comes from for each (or how it's determined). Also, what
    does the thing that uses those variables look like? Is it separate function
    calls, or a function that takes a list of arguments, or something else?

    Cheers,

    Tom

    On 18 November 2014 12:08, Douglas Houston <
    douglasrhouston at googlemail.com> wrote:
    Hi all,

    I'm a bit stuck with this so maybe someone can help me.

    I'm writing a little program that needs to iterate through several
    nested 'for' loops. However, the number of loops (and therefore the depth
    of the nesting) cannot be predetermined.

    I've read that lots of nested loops are bad form anyway, and that "you
    should do it with functions instead".

    However I still can't work out how to set the number dynamically.


    for ((l=0; l<10; l++)); do
    ? ?
    for ((m=0; m<10; m++))
    ?;?
    do

    ? ?
    for (n=0; n<10; n++)); do

    ? ?
    something
    ? that uses l, m and n?

    done
    done
    done

    So this is what it needs to look like if nest_depth=3

    But how do I write a program that iterates something
    ? for nest_depth=4 (which in this case would use l, m, n and o)?, or
    any other number (which can't be predicted)?

    cheers,
    Doug

    PS I didn't write this in Python just for speed, but feel free to
    reply in Python if you want - the practice would be good for me.

    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh
    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh
    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh
    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh
    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/edinburgh/attachments/20141118/2a5957ed/attachment-0001.html>
  • Magnus Hagdorn at Nov 18, 2014 at 12:56 pm
    Hi Doug,
    I think you will need to write an iterator which will return a tuple of
    indicies.


    How do you decide on the number of nested loops?


    Your program might looks something like


    def myIterator(some,args):
        white some condition:
            do some stuff to figure out what to do
            yield (i,j,k,...)


    for indicies in myIterator(...):
          do something with the tuple of indicies


    Hope this gives you a starting point


    Cheers
    magi



    On 18/11/14 12:08, Douglas Houston wrote:
    Hi all,

    I'm a bit stuck with this so maybe someone can help me.

    I'm writing a little program that needs to iterate through several
    nested 'for' loops. However, the number of loops (and therefore the
    depth of the nesting) cannot be predetermined.

    I've read that lots of nested loops are bad form anyway, and that "you
    should do it with functions instead".

    However I still can't work out how to set the number dynamically.


    for ((l=0; l<10; l++)); do
    ? ?
    for ((m=0; m<10; m++))
    ?;?
    do
    ? ?
    for (n=0; n<10; n++)); do
    ? ?
    something
    ? that uses l, m and n?

    done
    done
    done

    So this is what it needs to look like if nest_depth=3

    But how do I write a program that iterates something
    ? for nest_depth=4 (which in this case would use l, m, n and o)?, or any
    other number (which can't be predicted)?

    cheers,
    Doug

    PS I didn't write this in Python just for speed, but feel free to reply
    in Python if you want - the practice would be good for me.


    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh
  • James Doig at Nov 18, 2014 at 3:17 pm
    Hi Doug,
    Lists nested like this ( e.g: [1, [2, 3, [4 ,5 ,6 ,7]]] ) can be thought of
    as "tree" data structures.... Just incase you wanted to categorise the
    problem to aid future googling :)


    Cheers,


    James


    On 18 November 2014 12:56, Magnus Hagdorn wrote:

    Hi Doug,
    I think you will need to write an iterator which will return a tuple of
    indicies.

    How do you decide on the number of nested loops?

    Your program might looks something like

    def myIterator(some,args):
    white some condition:
    do some stuff to figure out what to do
    yield (i,j,k,...)

    for indicies in myIterator(...):
    do something with the tuple of indicies

    Hope this gives you a starting point

    Cheers
    magi


    On 18/11/14 12:08, Douglas Houston wrote:

    Hi all,

    I'm a bit stuck with this so maybe someone can help me.

    I'm writing a little program that needs to iterate through several
    nested 'for' loops. However, the number of loops (and therefore the
    depth of the nesting) cannot be predetermined.

    I've read that lots of nested loops are bad form anyway, and that "you
    should do it with functions instead".

    However I still can't work out how to set the number dynamically.


    for ((l=0; l<10; l++)); do
    ? ?
    for ((m=0; m<10; m++))
    ?;?
    do
    ? ?
    for (n=0; n<10; n++)); do
    ? ?
    something
    ? that uses l, m and n?

    done
    done
    done

    So this is what it needs to look like if nest_depth=3

    But how do I write a program that iterates something
    ? for nest_depth=4 (which in this case would use l, m, n and o)?, or any
    other number (which can't be predicted)?

    cheers,
    Doug

    PS I didn't write this in Python just for speed, but feel free to reply
    in Python if you want - the practice would be good for me.


    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh
    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/edinburgh/attachments/20141118/6868e409/attachment.html>
  • Douglas Houston at Nov 18, 2014 at 3:19 pm
    Plenty to be going on with there ... thanks everyone!


    On 18 November 2014 15:17, James Doig wrote:

    Hi Doug,
    Lists nested like this ( e.g: [1, [2, 3, [4 ,5 ,6 ,7]]] ) can be thought
    of as "tree" data structures.... Just incase you wanted to categorise the
    problem to aid future googling :)

    Cheers,

    James
    On 18 November 2014 12:56, Magnus Hagdorn wrote:

    Hi Doug,
    I think you will need to write an iterator which will return a tuple of
    indicies.

    How do you decide on the number of nested loops?

    Your program might looks something like

    def myIterator(some,args):
    white some condition:
    do some stuff to figure out what to do
    yield (i,j,k,...)

    for indicies in myIterator(...):
    do something with the tuple of indicies

    Hope this gives you a starting point

    Cheers
    magi


    On 18/11/14 12:08, Douglas Houston wrote:

    Hi all,

    I'm a bit stuck with this so maybe someone can help me.

    I'm writing a little program that needs to iterate through several
    nested 'for' loops. However, the number of loops (and therefore the
    depth of the nesting) cannot be predetermined.

    I've read that lots of nested loops are bad form anyway, and that "you
    should do it with functions instead".

    However I still can't work out how to set the number dynamically.


    for ((l=0; l<10; l++)); do
    ? ?
    for ((m=0; m<10; m++))
    ?;?
    do
    ? ?
    for (n=0; n<10; n++)); do
    ? ?
    something
    ? that uses l, m and n?

    done
    done
    done

    So this is what it needs to look like if nest_depth=3

    But how do I write a program that iterates something
    ? for nest_depth=4 (which in this case would use l, m, n and o)?, or any
    other number (which can't be predicted)?

    cheers,
    Doug

    PS I didn't write this in Python just for speed, but feel free to reply
    in Python if you want - the practice would be good for me.


    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh
    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh

    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/edinburgh/attachments/20141118/542242d1/attachment.html>
  • Magnus Hagdorn at Nov 18, 2014 at 3:43 pm
    well, if you have some tree like or more generally graph like data
    structure I'd use a data type that reflects this fact. I have been using
    the networkx python module for handling graphs.
    cheers
    magi



    On 18/11/14 15:17, James Doig wrote:
    Hi Doug,
    Lists nested like this ( e.g: [1, [2, 3, [4 ,5 ,6 ,7]]] ) can be thought
    of as "tree" data structures.... Just incase you wanted to categorise
    the problem to aid future googling :)

    Cheers,

    James

    On 18 November 2014 12:56, Magnus Hagdorn <magnus.hagdorn at marsupium.org
    wrote:

    Hi Doug,
    I think you will need to write an iterator which will return a tuple
    of indicies.

    How do you decide on the number of nested loops?

    Your program might looks something like

    def myIterator(some,args):
    white some condition:
    do some stuff to figure out what to do
    yield (i,j,k,...)

    for indicies in myIterator(...):
    do something with the tuple of indicies

    Hope this gives you a starting point

    Cheers
    magi



    On 18/11/14 12:08, Douglas Houston wrote:

    Hi all,

    I'm a bit stuck with this so maybe someone can help me.

    I'm writing a little program that needs to iterate through several
    nested 'for' loops. However, the number of loops (and therefore the
    depth of the nesting) cannot be predetermined.

    I've read that lots of nested loops are bad form anyway, and
    that "you
    should do it with functions instead".

    However I still can't work out how to set the number dynamically.


    for ((l=0; l<10; l++)); do
    ? ?
    for ((m=0; m<10; m++))
    ?;?
    do
    ? ?
    for (n=0; n<10; n++)); do
    ? ?
    something
    ? that uses l, m and n?

    done
    done
    done

    So this is what it needs to look like if nest_depth=3

    But how do I write a program that iterates something
    ? for nest_depth=4 (which in this case would use l, m, n and o)?
    , or any
    other number (which can't be predicted)?

    cheers,
    Doug

    PS I didn't write this in Python just for speed, but feel free
    to reply
    in Python if you want - the practice would be good for me.


    _________________________________________________
    Edinburgh mailing list
    Edinburgh at python.org <mailto:edinburgh@python.org>
    https://mail.python.org/__mailman/listinfo/edinburgh
    <https://mail.python.org/mailman/listinfo/edinburgh>


    _________________________________________________
    Edinburgh mailing list
    Edinburgh at python.org <mailto:edinburgh@python.org>
    https://mail.python.org/__mailman/listinfo/edinburgh
    <https://mail.python.org/mailman/listinfo/edinburgh>




    _______________________________________________
    Edinburgh mailing list
    Edinburgh at python.org
    https://mail.python.org/mailman/listinfo/edinburgh

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupedinburgh @
categoriespython
postedNov 18, '14 at 12:08p
activeNov 18, '14 at 3:43p
posts10
users6
websitepythonedinburgh.org

People

Translate

site design / logo © 2017 Grokbase