Is it possible to declare a variable that contains an if statement?

7

Is it possible to declare a variable that contains an if statement? The following pseudocode (written only with the idea of illustrating the example), represents the idea that is pursued, that is, that the variable m take a different value depending on the value of j.

j = [1, 2, 3]
m =  if j[0]:
            [{'b': 0.0, 'x': 0.0, 'm': 0.02} for _ in range(100)]
     if j[1]:
            [{'b': 0.5, 'x': 0.5, 'm': 0.02} for _ in range(100)]
     if j[2]:
            [{'b': 1.0, 'x': 1.0, 'm': 0.02} for _ in range(100)]
    
asked by pyring 15.08.2017 в 17:57
source

2 answers

5

I understand that what you are looking for is a quick assignment of a variable according to a certain condition. There are some ways to do it (I modified the values of your example a little because you were always returning the same list):

Through a function

def valor(j):

  if j == 1:
    return [{'b': 0.8, 'x': 0.5, 'm': 0.02} for _ in range(100)]
  elif j == 2:
    return [{'b': 1.8, 'x': 1.5, 'm': 1.02} for _ in range(100)]
  elif j == 3:
    return [{'b': 2.8, 'x': 2.5, 'm': 2.02} for _ in range(100)]
  else:
    return None

j = 2
m = valor(j)
print(m)

It is a good alternative because it allows to better verify the condition j , for example returning a default value if no expected value was chosen.

By using dictionaries

opciones = {1:[{'b': 0.8, 'x': 0.5, 'm': 0.02} for _ in range(100)],
            2:[{'b': 1.8, 'x': 1.5, 'm': 1.02} for _ in range(100)],
            3:[{'b': 2.8, 'x': 2.5, 'm': 2.02} for _ in range(100)]}


j = 2
m = opciones[j]
print(m)

It is a direct assignment from a dictionary that has as key the valid options, a KeyError will be emitted if you use any value of j not contemplated, unless you access the dictionary using the get() method:

m = opciones.get(j, None)
    
answered by 15.08.2017 / 19:01
source
4

Literally how you are asking is not possible. Even so, there are a couple of ways to do something very similar in Python. The first is with the so-called Coalescence Operators, and they are used in the following way:

m = j[0] or [{'b': 0.8, 'x': 0.5, 'm': 0.02} for _ in range(100)]

This is evaluated as follows. If the first term / variable is true, then the variable is assigned that first term / variable. If the first term / variable is false, then the variable is assigned the second term / variable. Keep in mind that only the first term / variable is evaluated.

The second one is with the ternary operator, which is quite similar. You should use it in the following way:

m = [{'b': 0.8, 'x': 0.5, 'm': 0.02} for _ in range(100)] if j[0] else None 

In this way, the default value is the list you are indicating when the condition is met and when the condition is not met, then the variable takes the value None .

As I was saying, it's not exactly doing an if within the assignment, but rather evaluating to assign. This is an unusual way to do it, but it is the closest thing to your question. I suppose you could somehow nest the operations in one of these two ways. Since you are probably investigating the means to reach a final goal, I recommend better to do it by functions as indicated by the Patricio response.

You can see more information about these operators in the official documentation of your Python version. and also in this question from the site in English .

    
answered by 15.08.2017 в 19:02