Ask Your Question
1

Checking that all values in an array satisfy an inequality

asked 2019-02-01 02:19:09 -0600

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 07:31:37 -0600

tmonteil gravatar image

updated 2019-02-01 12:25:45 -0600

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 07:28:21 -0600

rburing gravatar image

updated 2019-02-01 07:31:48 -0600

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 02:19:09 -0600

Seen: 57 times

Last updated: Feb 01