'how to use 'if i % number == 0:' with multiple numbers?
just a quick question, how would i make it so that instead of just having 1 number (5 in this example) i could have multiple numbers (e.g. 5 or 6 or 7 etc.) or would i just have to do all of them seperately.
This is my example - if (i % 5 == 0):
i know it seems like a dumb question but i can't figure it out for some reason unless it's not possible. I've already tried if (i % 5 or 6 == 0): and if i % (5 or 6) == 0: but neither of them worked.
hope this makes sense thank you in advance
Solution 1:[1]
You can use the 'mod' function from numpy, which will return the result as a list. And put the result in the any function, which will return True if at least one value has a remainder after division.
import numpy as np
i = 10
aaa = np.any(np.mod(i, [1, 2, 3]))
print(aaa)
Output
True
Results the remainder is zero everywhere.
i = 10
aaa = np.any(np.mod(i, [1, 2, 5]))
print(aaa)
Output
False
Solution 2:[2]
You can use the RxJs Conditional Operator iff to achieve that if both of your services return an Observable or a Promise.
The iff operator takes 3 arguments, first when being your condition, 2nd and 3rd being your different services that return an Observable/Promise.
If the condition is true subscribe to the first observable, if it is false subscribe to the second observable
iif(
() => {
//Add your condition here
return a + b === 4;
},
this.someService.someMethodWhenTrue(),
this.someService.someMethodWhenFalse()
)
.pipe(takeUntil(this.unsubscribe$))
.subscribe((items)=> {
this.someData = items;
});
I recommend you to read this https://www.learnrxjs.io/learn-rxjs/operators/conditional/iif
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | HassanMoin |
