简体   繁体   English

将乘法结果放入列表中

[英]Putting Result of Multiplication in a List

I am trying to put a series of multiplications inside a list, I am using the code below:我正在尝试将一系列乘法放在列表中,我正在使用以下代码:

listx = []
for i in range (2):
    list = [(3*i)]
    listx.append(list)

The problem is that this will put the two results inside two separate lists inside a lists, I just wants the floats to be inside the first list.问题是这会将两个结果放在一个列表中的两个单独列表中,我只希望浮点数位于第一个列表中。

listx = []
for i in range (2):
    listx.append(3*i)

Just use this one.就用这个吧。 There is no need to create another list for storing the result.无需创建另一个列表来存储结果。 You created another list to store the value and appended that list into your listx您创建了另一个列表来存储值并将该列表附加到您的listx

You can also use list comprehensions.您还可以使用列表理解。 Basically it's the same with for cycles but it's much shorter and for simpler operations like yours it's easier to read.基本上它与 for 循环相同,但它更短,并且对于像您这样的更简单的操作,它更容易阅读。

listx = [i*3 for i in range(2)]

This should produce a single list with values multiplied by 3 as integers这应该生成一个列表,其中的值乘以 3 作为整数

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

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