简体   繁体   中英

What does this line do? (using num and enumerate)

I'm a python beginner, and have been given this code to write out what each line does. Here is the code:

z = [int(input("Enter your nth term sequence ")) for _ in range(12)]
total = sum([num * 8 if i % 2 == 0 else num * 4 for i, num in enumerate(z)])

So, I assume that z is a list, where a user inputs the numbers of the nth term sequence, there are 12 digits which they can enter. The code then finds a sum of the numbers multiplied by 8 if something divided by 2 gives a remainder of 0. I just don't get what ([num * 8 if i % 2 == 0 else num * 4 for i, num in enumerate(z)]) means. I've tried and have attempted to search things up, but it's just too confusing. Please help if possible

The code:

foo = [num * 8 if i % 2 == 0 else num * 4 for i, num in enumerate(z)]

is functionally equivalent to this:

foo = []
for i, num in enumerate(z): # num is element in z, i is index of num
    if i % 2 == 0:
        foo.append(num*8)
    else:
        foo.append(num*4)

Then the user calls sum on the result.
Does this answer your question?


EDIT: explain enumerate

You should get used to reading documentation on functions, for example enumerate . After you've read it, if you still don't understand then you should search stackoverflow. For example: A post asking what enumerate is with a good answer

Calling:

for num in z:
    i = z.index(num)
    # Do stuff

is functionally the same as saying:

for i, num in enumerate(z):
    # Do stuff

Lets break this down step by step.

z = [int(input("Enter your nth term sequence ")) for _ in range(12)]

First, your assumption that z is a list is correct. It is generated from a list comprehension. It gets input from a user with input("Enter your nth term sequence ") a total of 12 times using for _ in range(12) . So z will be a list of 12 user-input integers (not digits, as it can be any integer, not just 0-9).

Next we have:

total = sum([num * 8 if i % 2 == 0 else num * 4 for i, num in enumerate(z)])

It's immediately clear that we are generating a total from the sum of a list with total = sum([...]) . So next we need to figure out the contents of the list generated by the list comprehension.

To make the list comprehension a little bit easier to understand we can add in some parentheses to break up the comprehension into easier to manage parts which we will then handle individually.

(num * 8) if (i % 2 == 0) else (num * 4) for (i, num) in (enumerate(z))

The first three groups are fairly self-explanatory:

num * 8 eight times a number i % 2 == 0 check if i is even num * 4 four times a number

So the first half of the list comprehensions returns a value that is eight times a number if i is even or four times that number if it isn't.

So that leaves us with the last half of the list comprehension for i, num in enumerate(z) .

Lets discuss enumerate first. Enumerate takes a sequence and numbers each element in that sequence. For example enumerate(['a', 'b', 'c']) would return a list of 2-element tuples [(0, 'a'), (1, 'b'), (2, 'c')] .

The for i, num in takes the tuples from enumerate(z) and unpacks each tuple into i and num . So i is the index generated by enumerate and num is the original user input.

TL;DR

z = [int(input("Enter your nth term sequence ")) for _ in range(12)]
total = sum([num * 8 if i % 2 == 0 else num * 4 for i, num in enumerate(z)])

Line 1 gets 12 integers from user input and stores them in z

Line 2 takes those integers, indexes them (starting at 0), and if the index is even multiplies that number by 8, if the index is odd multiplies that number by 4.

Finally line 2 sums all the multiplied user inputs and stores them in total

Well lets break it down. You are storing something to the variable "total".

Then you are taking the Sum of everything inside your parentheses. You run an if statement, saying if i modulus 2(ie if it is divisible by two and has no remainder left over) append num(a variable) times 8 to the list z.

else, num times 4 appended to the list z. Its a compact way that they wrote out the statement instead of multiple lines

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM