简体   繁体   English

Python创建了一个X元素列表,其中的元素按Y计数。例如,如果X = 4且Y = 5,您将收到[5、10、15、20]

[英]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的索引?

So the problem you are having is the range of numbers you are generating: 因此,您遇到的问题是生成的数字范围:

>>> x = 4
>>> list(range(x))
[0, 1, 2, 3]

Just change 只是改变

range(1, x + 1)

And see the documentation for range . 并参阅range的文档。

仅使用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.

相关问题 Python 如何在列表 X 中搜索元素 Y。元素 Y 是一个列表本身,其元素可能与 X 中列出的顺序不符 - Python How to search through list X for element Y. Element Y is a list itself whose elements may not be in order as listed in X 创建包含列表x但不包含列表y中与列表x相同的元素的新列表-python - Create new list containing list x but not elements in list y that are the same as in list x - python 计算 x % y == 0 的出现次数 - Count occurences where x % y == 0 我打印图片 X。然后我尝试打印图片 Y,但它再次打印图片 X,然后是 Y。请问,如何从 Python 列表中删除图片 X? - I print Picture X. I then try to print Picture Y but it prints Picture X again followed by Y. Please, how can I delete Picture X from a Python list? 计算一列中大于x但小于y的元素数 - Count number of elements in a column greater than x but smaller than y PYTHON:解决 y=a+b*x 其中 x 是预定义列表 - PYTHON: solving y=a+b*x where x is a predefined list 在 Python 中使用 2D 掩码从 (x,y) 字段中有效地选择元素 - Efficiently select elements from an (x,y) field with a 2D mask in Python 将list = {“ xy”,“ wz”,“ ab”}转换为dict = {x:y。 w:z,a:b} - convert list = {“x-y”, “w-z”, “a-b”} into dict={x:y. w:z,a:b} Numpy 3D 数组的维度顺序设计为 z、x、y。 有什么优势吗? - The dimension orders of the Numpy 3D array are designed to z, x, y. Are there any advantages? 如何使用Python在网格中创建10个随机的x,y坐标 - How to create 10 random x, y coordinates in a grid using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM