I have a range of values (L,R,U,D
) and two variables, d
and newd
, containing one of them. I need to check if d
and newd
are in the same subset (L,R
or U,D
) or not.
I know I can do this:
d in {'L','R'} and newd in {'U','D'} or d in {'U','D'} and newd in {'L','R'}
this indeed returns False
if they both have values in L,R
or U,D
, and True
otherwise. Still, I find it much reduntant. Some suggestions about a more DRY approach?
Summary. The referential equality (using === , == or Object.is() ) determines whether the operands are the same object instance.
A variable is a characteristic that can be measured and that can assume different values. Height, age, income, province or country of birth, grades obtained at school and type of housing are all examples of variables.
Use the == operator to test if two variables are equal.
To test multiple variables x , y , z against a value in Python, use the expression value in {x, y, z} . Checking membership in a set has constant runtime complexity. Thus, this is the most efficient way to test multiple variables against a value.
If you know that there are only two sets and that your values must be in one or the other, then you can simplify it to this:
(d in set1) == (newd in set2)
Explanation:
==
are True
, so the expression returns True
.==
are False
, so the expression returns True
.==
will return False
and the other True
so the result of the expression will be False
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With