[英]Python create a list of X elements where the elements count by Y. For example, if X = 4 and Y = 5 you'd receive [5, 10, 15, 20]
This is what I have tried so far: 到目前为止,这是我尝试过的:
x = 4
y = 5
z = []
for i in range(x):
i=i*y
z.append(i)
But my output is showing: [0, 5, 10, 15] 但是我的输出显示:[0、5、10、15]
How do I get indexing to start on 5 and go to 20? 如何获得从5开始到20的索引?
仅使用range
方法即可生成列表。
z = list(range(y, x*y + 1, y))
You can add one to i
您可以添加一个到i
x = 4
y = 5
z = []
for i in range(x):
i=(i+1)*y
z.append(i)
You can use a list comprehension as well. 您也可以使用列表推导。
x = 4
y = 5
z = [y + i * y for i in range(x)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.