How to Check Whether Multiple Variables Are Equal to the Same Value

Check if multiple variables have the same value

If you have an arbitrary sequence, use the all() function with a generator expression:

values = [x, y, z]  # can contain any number of values
if all(v == 1 for v in values):

otherwise, just use == on all three variables:

if x == y == z == 1:

If you only needed to know if they are all the same value (regardless of what value that is), use:

if all(v == values[0] for v in values):

or

if x == y == z:

How to test multiple variables for equality against a single value?

You misunderstand how boolean expressions work; they don't work like an English sentence and guess that you are talking about the same comparison for all names here. You are looking for:

if x == 1 or y == 1 or z == 1:

x and y are otherwise evaluated on their own (False if 0, True otherwise).

You can shorten that using a containment test against a tuple:

if 1 in (x, y, z):

or better still:

if 1 in {x, y, z}:

using a set to take advantage of the constant-cost membership test (i.e. in takes a fixed amount of time whatever the left-hand operand is).

Explanation

When you use or, python sees each side of the operator as separate expressions. The expression x or y == 1 is treated as first a boolean test for x, then if that is False, the expression y == 1 is tested.

This is due to operator precedence. The or operator has a lower precedence than the == test, so the latter is evaluated first.

However, even if this were not the case, and the expression x or y or z == 1 was actually interpreted as (x or y or z) == 1 instead, this would still not do what you expect it to do.

x or y or z would evaluate to the first argument that is 'truthy', e.g. not False, numeric 0 or empty (see boolean expressions for details on what Python considers false in a boolean context).

So for the values x = 2; y = 1; z = 0, x or y or z would resolve to 2, because that is the first true-like value in the arguments. Then 2 == 1 would be False, even though y == 1 would be True.

The same would apply to the inverse; testing multiple values against a single variable; x == 1 or 2 or 3 would fail for the same reasons. Use x == 1 or x == 2 or x == 3 or x in {1, 2, 3}.

What is a shorthand way for checking that multiple variables are ALL equal to the same value in an IF statement? (PHP)

array_flip is several times faster than array_unique:

function all_equal($arr, $value) {
return array_keys(array_flip($arr)) == array($value);
}

$arr = array($tstat, $l1stat, $l2stat, $l3stat);

echo all_equal($arr, 'no_prices');

A quick profile for the answers given thus far, for 1000 iterations on array length 1000:

  array_flip: 0.07321620 seconds
array_unique: 0.32569408 seconds
foreach: 0.15136194 seconds
array_filter: 0.41404295 seconds

The code used to profile is here: http://codepad.org/szgNfWHe

Note: As @cypherabe rightly points out, array_flip does not overtake array_unique until the array has at least 5 elements, and does not overtake foreach until the array has at least 10 elements.

Cross check if multiple variable are equal in php

$v1 = 1;
$v2 = 2;
$v3 = 3;
$v4 = 4;
$v5 = 5;
$v6 = 5;
$values = [$v1, $v2, $v3, $v4, $v5, $v6];
if (count($values) !== count(array_unique($values))) {
echo 'Duplicates found';
}

DEMO

UPDATED

You can easily use array_count_values function to determine which values are duplicated.
I put it here: http://phpio.net/s/16bh

How can I check whether multiple variables are equal to the same value?

if((A == 'X' || A == 'O') && A == B && B == C)
{
// Do whatever
}

How to compare multiple variables to the same value?

You want to test a condition for all the variables:

if all(x >= 2 for x in (A, B, C, D)):
print(A, B, C, D)

This should be helpful if you're testing a long list of variables with the same condition.


If you needed to check:

if A, B, C, or D >= 2:

Then you want to test a condition for any of the variables:

if any(x >= 2 for x in (A, B, C, D)):
print(A, B, C, D)

R Check if multiple variables with the same pattern have the same values

We could split the data into a list of data.frame based on the prefix names and then use == by comparing the first column with all other columns after looping over the list with sapply. Wrap with all to check if we have all TRUEs

sapply(split.default(df, sub("\\d+$", "", names(df))), function(x) all(x[,1] == x))
# a b nn o u
#TRUE TRUE TRUE TRUE TRUE

If we need only to compare 'a' columns

dfa <- df[startsWith(names(df), 'a')]
all(dfa == dfa[,1])
#[1] TRUE

Checking if multiple variables are all the same value in R

Use list instead of c when creating your group to test against:

w <- x <- y <- z <- NULL

sapply(list(w,x,y,z), is.null)
#[1] TRUE TRUE TRUE TRUE

all(sapply(list(w,x,y,z), is.null))
#[1] TRUE

seems to work.

As to why c doesn't work, consider:

c(NULL,NULL,1,NULL)
#[1] 1
c(NULL,NULL,NULL)
#NULL


Related Topics



Leave a reply



Submit