[Tutor] Making big 'uns into little 'uns

Walter Prins wprins at gmail.com
Thu Sep 6 17:29:08 CEST 2012


Hi Ray,

On 6 September 2012 15:59, Ray Jones <crawlzone at gmail.com> wrote:
> Basically it's as simple as ensuring that an array consists of integers,
> and that those integers fall within a certain range. Rather than using
> multiple 'if' statements, I was (am, at this point) using multiple tests
> within a single 'if' statement. Nothing earth-shatteringly difficult,
> but I was simply looking for a way to shorten the overall test
> expression with a recursive(? is that the term) variable. No problem though.

By array I suppose you mean "list of lists of items"?

Anyway, if you have such a structure, and you want to "visit" each in
turn to check it, you can do this:

for sublist in grid:
    for item in sublist:
        # code to check if "item" is in range goes here

The above obviously doesn't actually track the "row" or "column"
you're checking.  If you'd like to keep track of what "row"/"column"
you're on, you can for example do:

for row, sublist in enumerate(grid):
    for col, item in enumerate(sublist):
        # code to check if "item" is in range goes here

HTH,

Walter


More information about the Tutor mailing list