Ask Your Question
1

Checking that all values in an array satisfy an inequality

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

anonymous user

Anonymous

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
1

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

tmonteil gravatar image

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

You can use any of all as follows:

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

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

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

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

rburing gravatar image

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

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

Stats

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

Seen: 476 times

Last updated: Feb 01 '19