Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not totally sure I've interpreted your problem correctly; however what follows is a fairly general exercise which should serve to enlighten you somewhat either way. For the purposes of discussion here I'll assume that you're aiming at doing an arbitrary number of nested for-loops.</p> <p>First, though, let me just satisfy the Pythonistas reading this by saying that the Pythonic way of doing this probably involves using the function <a href="http://docs.python.org/library/itertools.html#itertools.product" rel="nofollow">itertools.product</a> and probably the built-in <code>filter()</code> function.</p> <p>That aside, I think you should familiarize yourself with the concept of <strong>recursion</strong>, because it allows you to sort of "naively" come up with simple solutions to problems like this without having to rely on a sweet-ass language like Python to give you an awesome library full of generator functions.</p> <p>Let's say you've got some lists in some list-of-lists, like:</p> <pre><code>nested = [[a1, a2, a3, ..., ana], [b1, b2, b3, ..., bnb], ... [x1, x2, x3, ..., xnx]] </code></pre> <p>And you want to iterate <em>pseudo-lexographically</em> through sequences which contain a single element from each of these lists, like:</p> <pre><code>a1 b1 c1 ... x1 a1 b1 c1 ... x2 ... a2 b1 c1 ... x1 a2 b1 c1 ... x2 ... ... ana bnb cnc ... x(nx - 1) ana bnb cnc ... xnx </code></pre> <p>It's tough to do this with normal static for-loops because you usually have to type each one out, right? How do you type out some variable number of for-loops? A sort of naive trick to do this is using <strong>recursion</strong> (again this is not the most Pythonic way).</p> <p>Recursion says, "okay, I have a problem. This problem is basically just doing something small with a part of my data, and then <strong><em>recursively</em></strong> solving the same problem on the rest of the data. When I have no more data to work with, I'm probably done with this sub-problem."</p> <p>A simple, classic example of recursion is in computing a sum of a list of numbers:</p> <pre><code>def sum_nums(nums): # The "base case" is usually checked first. This is when your # recursion has run out of things to do, it just returns a simple value. if nums == []: return 0 # The "recursive case" is the real meat and potatoes of recursion. # It says to take the first number and add it to the sum_nums of the # rest of the numbers in the list (which may be empty, hence the base case!). else: return nums[0] + sum_nums(nums[1:]) </code></pre> <p>Think about this. Understand it. Meditate on it, and forget that you understand it. Do some psychedelics and think about it again. It's pretty important to be able to pull recursive hacks out of your ass on the fly.</p> <p>Now a more advanced technique is to pass down values in a recursive function. In this case it can be useful because you don't want to do anything until you get down to the base case. I've left out the base case for you to implement yourself, if you care to do so.</p> <pre><code>def print_lexo_lists(items, lists): if lists == []: # What do you want to do if you've taken a single item from each of your # lists and put it in the list called "items"? else: # Take each item from the current level and pass it down in turn to the # next deeper level, adding it to the running list of "items". Remove # the current level from the list of "lists" before passing it down. # In this way you're "using up" the current level, and the "lists" # structure is getting smaller at each level of recursive depth, until # you reach the base case (when it is an empty list) for current_item in lists[0]: print_lexo_lists(items + [current_item], lists[1:]) </code></pre> <p>Hope you can follow that to some extent. I highly recommend that you implement that before trying the following way, which is really the <em>correct</em> way of doing uber-nested for-loops in Python by generating the <strong><em>Cartesian Product</em></strong> of the lists:</p> <pre><code>from itertools import product for items in product(*lists): print ' '.join(items) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload