Ask Your Question

Checking that all values in an array satisfy an inequality

asked 2019-02-01 09:19:09 +0200

anonymous user


I would like to know how to check element-wise that every entry in an array is less than or equal to a particular number. It would seem that something like this would require a loop, but I am not sure. Could someone help me? Thank you in advance

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2019-02-01 14:31:37 +0200

tmonteil gravatar image

updated 2019-02-01 19:25:45 +0200

You can use any of all as follows:

sage: L = [1,2,3,4,5]
sage: all(x < 10 for x in L)

sage: all(x < 3 for x in L)

sage: any(x < 3 for x in L)
edit flag offensive delete link more

answered 2019-02-01 14:28:21 +0200

rburing gravatar image

updated 2019-02-01 14:31:48 +0200

No explicit loop is necessary; you can use a generator expression in combination with all. If L is the list and c is the particular number, you can do:

all(el <= c for el in L)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-02-01 09:19:09 +0200

Seen: 396 times

Last updated: Feb 01 '19