Python Forum
Multiplying between two values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiplying between two values
#7
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!
Reply


Messages In This Thread
Multiplying between two values - by doug2019 - Oct-19-2020, 10:14 PM
RE: Multiplying between two values - by bowlofred - Oct-19-2020, 10:21 PM
RE: Multiplying between two values - by jefsummers - Oct-19-2020, 11:14 PM
RE: Multiplying between two values - by doug2019 - Oct-19-2020, 11:17 PM
RE: Multiplying between two values - by bowlofred - Oct-19-2020, 11:31 PM
RE: Multiplying between two values - by deanhystad - Oct-20-2020, 04:42 AM
RE: Multiplying between two values - by DeaD_EyE - Oct-20-2020, 08:03 AM
RE: Multiplying between two values - by doug2019 - Oct-20-2020, 05:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  List structure lost when multiplying Protonn 2 2,306 Apr-23-2020, 04:16 AM
Last Post: buran
  multiplying elements in a list Olavv 3 3,483 Feb-27-2020, 04:55 PM
Last Post: DeaD_EyE
  Multiplying two lists to make an array pberrett 15 6,002 May-15-2019, 02:22 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020