[Tutor] Making big 'uns into little 'uns
Oscar Benjamin
oscar.j.benjamin at gmail.com
Thu Sep 6 17:34:06 CEST 2012
On 2012-09-06, 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.
>
> Thanks.
How about using a generator expression with all()? For example:
>>> def isint(n):
... return int(n) == n
...
>>> def isinrange(n, a, b):
... return a <= n <= b
...
>>> numbers = [11, 12, 14, 15]
>>> all(isint(x) and isinrange(x, 10, 20) for x in numbers)
True
>>> numbers[0] = 9
>>> numbers
[9, 12, 14, 15]
>>> all(isint(x) and isinrange(x, 10, 20) for x in numbers)
False
>>> numbers[0] = 14.5
>>> all(isint(x) and isinrange(x, 10, 20) for x in numbers)
False
Oscar
More information about the Tutor
mailing list