-2

Can someone explain the following loop code converted into a one line code?

Code:

total_price = 0
for cost in gift_costs:
    if cost < 25:
        total_price += cost * 1.08

Converted one line code:

total_price = (gift_costs[gift_costs < 25]).sum() * 1.08
alani
  • 12,573
  • 2
  • 13
  • 23
pearl_destiny
  • 11
  • 1
  • 2
  • 2
    "please explain this code" questions are typically off-topic here, unless you specify a very specific aspect you don't understand and there aren't any questions about that aspect already answered elsewhere on the site. – Charles Duffy Jun 28 '20 at 01:19
  • 6
    Moreover, it's important for code to be a [mcve] -- the shortest possible code someone can copy-and-paste without changes to see the behavior you're asking about. If we just set `gift_costs=[10 20 30 40]` your code [doesn't work](https://ideone.com/wMtpEF). I need to assume that your real `gifts_cost` is an object created using a library that isn't specified in the question. – Charles Duffy Jun 28 '20 at 01:21
  • Read about list comprehension in Python – Rafael Almeida Jun 28 '20 at 01:24
  • 2
    @RafaelAlmeida, ...except this code _isn't_ a list comprehension. (I first closed the question as a duplicate of one on the subject, since that's the obvious assumption given the title and context -- but take a closer look; it's not, hence being reopened). – Charles Duffy Jun 28 '20 at 01:25
  • are these numpy arrays? – malanb5 Jun 28 '20 at 01:28
  • I would suggest breaking the code into smaller portions, running them and seeing the output. The code seems self explainable. – pecey Jun 28 '20 at 01:28
  • @Charles Duffy The initialization should be `gift_costs=[10, 20, 30, 40]`. You missed the commas and there is a syntax error. – xuhdev Jun 28 '20 at 01:34
  • @Charles fair enough, my mistake. If those are numpy arrays it would make sense, but yes, the question should have the full code – Rafael Almeida Jun 28 '20 at 02:12
  • @xuhdev, still fails later even with that correct, though, unless one uses numpy arrays. – Charles Duffy Jun 28 '20 at 03:08

3 Answers3

1

You are not explicitly saying it, but it's pretty much clear that you're using numpy arrays. So let's break that line down, taking some arbitrary array as an example:

>>> gift_costs = numpy.array([10, 22, 35, 41])
>>> total_price = (gift_costs[gift_costs < 25]).sum() * 1.08
>>> total_price
34.56
  1. total_price = : What is on the right side of the expression is going to be assigned to the total_price variable.
  2. gift_costs < 25 : Whenever you use a comparison operator (such as <) along with a numpy array, that conditional statement is going to be evaluated for each item of the array. Then, you're going to get a new array filled with boolean values, which are going to be True if a given value matches the condition, or False otherwise. So, for the above example, you would get the following: array([True, True, False, False])
  3. gift_costs[gift_costs < 25] : Considering what we have just mentioned above, we are now asking gift_costs to return its values which position in the following sequence are True: [True, True, False, False]. Those elements are: 10 and 22, and the returned object is: array([10, 22])
  4. .sum() : Numpy array objects have a method called sum(), which returns the summation (∑) of the array. In our example, this is 10 + 22 = 32.
  5. * 1.08 : Finally, the value returned by the sum() method (32) is multiplied by 1.08, which equals to 34.56

Disclaimer: Excuse me for my English, I tried to make myself clear the best I could.

revliscano
  • 2,227
  • 2
  • 12
  • 21
0

We can also code in a line without numpy. Plz check this one.

l = [10, 22, 35, 41]

t = sum((x for x in l if x <25))*1.08

I used sum function and make a generator for the argument of the function.
The equation of generator is simple. Just read it as followings.
"take an x in list l if it is less than 25"
The generator gathering all the x in the condition and then transfer to the sum function...

Looks simple, doesn't it???

hochae
  • 99
  • 4
0

It's basically the same thing. The for loop uses an if clause to identify cost <25 and subsequently applies to them the multiplication * 1.08. The one clause filters using the [gift_costs < 25] and subsequently applies the same multiplication. Another possibility (also more efficient in terms of computing time) is a list comprehension.

Michele Giglioni
  • 329
  • 4
  • 15