Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test multiple variables against a single value?

I'm trying to make a function that will compare multiple variables to an integer and output a string of three letters. I was wondering if there was a way to translate this into Python. So say:

x = 0 y = 1 z = 3 mylist = []  if x or y or z == 0 :     mylist.append("c") if x or y or z == 1 :     mylist.append("d") if x or y or z == 2 :     mylist.append("e") if x or y or z == 3 :      mylist.append("f") 

which would return a list of:

["c", "d", "f"] 

Is something like this possible?

like image 613
user1877442 Avatar asked Feb 27 '13 12:02

user1877442


People also ask

How do you test a variable against multiple values in Python?

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.

How do you declare multiple variables with the same value?

You can assign the same value to multiple variables by using = consecutively. This is useful, for example, when initializing multiple variables to the same value. It is also possible to assign another value into one after assigning the same value.

How do you check for multiple equality in Python?

Python Code: x = 20 y = 20 z = 20 if x == y == z == 20: print("All variables have same value!") Sample Output: All variables have same value!

How do you check if a value is equal to multiple values?

To check if a variable is equal to all of multiple values, use the logical AND (&&) operator to chain multiple equality comparisons. If all comparisons return true , all values are equal to the variable. Copied! We used the logical AND (&&) operator to chain multiple equality checks.


2 Answers

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}.

like image 125
Martijn Pieters Avatar answered Sep 22 '22 17:09

Martijn Pieters


Your problem is more easily addressed with a dictionary structure like:

x = 0 y = 1 z = 3 d = {0: 'c', 1:'d', 2:'e', 3:'f'} mylist = [d[k] for k in [x, y, z]] 
like image 39
dansalmo Avatar answered Sep 24 '22 17:09

dansalmo