Oct-20-2020, 08:03 AM
Quote:I need an equation in Python that gives me the values that can be multiplied, since they must be less than 10.
List comprehension with a conditional which uses the named assignment expression:
x_values = [1,2,3,4,5] y_values = [10,5,3.33,2.5,2] xy_values = [value / 10 for x, y in zip(x_values, y_values) if (value := x * y) < 10]To understand the list comprehension a bit better, here the multiline version:
xy_values = [ value / 10 for x, y in zip(x_values, y_values) if (value := x * y) < 10 ]Of course, you can use a naive for-loop to solve it and you don't have always the latest Python version available (walrus operator).
xy_values = [] for x, y in zip(x_values, y_values): xy = x * y if xy < 10: xy_values.append(xy / 10)The walrus operator was used in the if-statement:
xy_values = [] for x, y in zip(x_values, y_values): if (xy := x * y) < 10: xy_values.append(xy / 10)If you play with the factor, you can get more values. I get only one float as result.
Output:[0.999]
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!