简体   繁体   English

这条线是做什么的? (使用num和enumerate)

[英]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. 我是python的初学者,并且已获得此代码以写出每一行的功能。 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. 因此,我假设z是一个列表,用户在其中输入第n个术语序列的数字,他们可以输入12个数字。 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. 然后,代码将找到数字乘以8(如果将某物除以2得到的余数为0)的总和。我就是不知道([num * 8 if i % 2 == 0 else num * 4 for i, num in enumerate(z)]) 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. 然后,用户对结果sum
Does this answer your question? 这回答了你的问题了吗?


EDIT: explain enumerate 编辑:解释枚举

You should get used to reading documentation on functions, for example enumerate . 您应该习惯于阅读有关功能的文档,例如enumerate After you've read it, if you still don't understand then you should search stackoverflow. 阅读后,如果仍然不了解,则应搜索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. 首先,您假设z是一个列表是正确的。 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) . 它使用input("Enter your nth term sequence ")从用户那里获得输入,共12次,使用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). 因此z将是12个用户输入整数的列表(不是数字,因为它可以是任何整数,而不仅仅是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([...]) . 显而易见,我们从具有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 num * 8个数字的八倍i % 2 == 0检查i是否为num * 4个数字的四倍

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. 因此,列表推导的前半部分返回的值是i 8倍,如果i是偶数,则4倍。

So that leaves us with the last half of the list comprehension for i, num in enumerate(z) . 这样一来,我们就可以对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')] . 例如enumerate(['a', 'b', 'c'])将返回一个2元素元组的列表[(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 . for i, num inenumerate(z) for i, num in获取元组,并将每个元组解包为inum So i is the index generated by enumerate and num is the original user input. 因此, i是由enumerate生成的索引,而num是原始用户输入。

TL;DR 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 第1行从用户输入中获取12个整数并将其存储在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. 第2行采用这些整数,对其进行索引(从0开始),如果索引为偶数将该数字乘以8,如果索引为奇数,则将该数字乘以4。

Finally line 2 sums all the multiplied user inputs and stores them in total 最后,第2行将所有相乘的用户输入求和并total存储

Well lets break it down. 好吧,让我们分解一下。 You are storing something to the variable "total". 您正在将某些内容存储到变量“ 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. 您运行一条if语句,说是否i模2(即是否可以被2整除并且没有剩余的余数)将num(一个变量)乘以8到列表z。

else, num times 4 appended to the list z. 否则,将num乘以4到列表z。 Its a compact way that they wrote out the statement instead of multiple lines 他们写出语句而不是多行的紧凑方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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