简体   繁体   English

如何在 Python 中创建按 2 计数的 x 元素列表

[英]How to create a list of x elements counting by 2 in Python

I'm taking an introductory Python course, so this question might be pretty easy for most of you all.我正在学习 Python 入门课程,所以这个问题对你们中的大多数人来说可能很容易。 An assignment I'm working on is introducing us to lists.我正在做的一项任务是向我们介绍列表。 We are given the variable b2 = 5 and are tasked with using b2 to create a list c2 such that c2 = [0, 2, 4, 6, 8] which has b2 number of elements and counts by 2. How would I do this?给定变量 b2 = 5,任务是使用 b2 创建一个列表 c2,使得 c2 = [0, 2, 4, 6, 8] 有 b2 个元素,计数为 2。我该怎么做?

Lets break down the problem.让我们分解问题。 First let us cover how lists are created in python.首先让我们介绍如何在 python 中创建列表。

#will create a list variable
c2 = [] 

Each list variable has the append method that you can readily use.每个列表变量都有您可以轻松使用的 append 方法。 So in the event you need to add the first element into the list, just append it to the list that contains no values.因此,如果您需要将第一个元素添加到列表中,只需将其附加到不包含任何值的列表中。

c2.append(value)

The thing is, your problem will probably want you to automatically increment the counter and place the new value, without having to hard code each value into the list.问题是,您的问题可能希望您自动增加计数器并放置新值,而不必将每个值硬编码到列表中。 This is how to solve the problem if you decide to hardcode.如果您决定进行硬编码,这就是解决问题的方法。

c2.append(0)
c2.append(2)
c2.append(4)
c2.append(6)
c2.append(8)

Now the reason the question gives you value b2 = 5 is because that is your limit, you want to loop, increment a counter by a value of 2 until you have incremented the counter by 2 five times.现在问题给你值 b2 = 5 的原因是因为这是你的限制,你想循环,将计数器的值增加 2,直到你将计数器增加 2 5 次。

Increment of a counter can be done as such:计数器的增量可以这样完成:

b2 = 0
b2 =+ 2 #or any numerical value you would like to increment by
#b2's new value will be 2 if you print it. 

I assume you have already covered loops in this course you are taking.我假设您已经在本课程中学习了循环。 Take the info I covered and place them into a for loop, using your b2 as your limit, your knowledge of increment by 2 every time the loop comes around, and then append the new counter value into the list.获取我涵盖的信息并将它们放入 for 循环中,使用 b2 作为限制,每次循环出现时您对增量的了解,然后将新的计数器值附加到列表中。

If you want go ahead and update your code with what you think is your answer and we can work through it, any questions feel free to commend and we can go over then.如果您想继续使用您认为的答案更新您的代码并且我们可以解决它,任何问题都可以随时提出建议,然后我们可以继续。

There are much faster ways of answering the question such as:有更快的方法来回答这个问题,例如:

c2 = range(0,b2*2,2)

The range function works like this . range 函数的工作原理是这样的 range() can take 3 parameters: range() 可以接受 3 个参数:

  1. start: staring number in the sequence开始:序列中的起始编号
  2. stop: Generate numbers up to, but not including this number, hence why you multiply b2 * 2 in order to generate numbers up to 10 but not 10 leaving the last number at 8 due to the next parameter.停止:生成最多但不包括这个数字的数字,因此为什么你乘 b2 * 2 以生成最多 10 但不是 10 的数字,由于下一个参数,最后一个数字为 8。
  3. Step: Difference between each number in sequence步骤:序列中每个数字之间的差异

    range(start,stop,step)范围(开始,停止,步骤)

But I assume the problem wants you to get familiar with using for loops, list functions, and operators.但我认为这个问题需要您熟悉使用 for 循环、列表函数和运算符。

b2=5
c2=[]
for i in range(b2):
    i=i*2
    c2.append(i)
#Print for Pyton 2:
print c2
#Print for Python 3:
print(c2)

Variable i will be 0 when is first appended to c2, than it will take the value 2 (range(b2) is same as range(5) is same as [0,1,2,3,4]) and so on while there are values to take.变量 i 在第一次附加到 c2 时将为 0,而不是取值 2 (range(b2) 与 range(5) 与 [0,1,2,3,4] 相同)等等,而有一些价值观可以接受。 When the loop is finished it will print out c2.当循环完成时,它将打印出 c2。 Hope I helped you :D希望我能帮到你 :D

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

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