Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在for循环中填充空列表?_Python_List - Fatal编程技术网

Python 如何在for循环中填充空列表?

Python 如何在for循环中填充空列表?,python,list,Python,List,我试着在代码中这样做,就像用更多的列表填充一个空列表 #These are the components I'm gonna use. I'm not using all of them. nc3=[-970689, 7.151, -.7698, 13.7097, 1872.82, -25.1011] ic4=[-1166846, 7.727, -0.9221, 13.8137, 2150.23, -27.6228] nc4=[-1280557, 7.95, -0.

我试着在代码中这样做,就像用更多的列表填充一个空列表

#These are the components I'm gonna use. I'm not using all of them.
     nc3=[-970689, 7.151, -.7698, 13.7097, 1872.82, -25.1011]
     ic4=[-1166846, 7.727, -0.9221, 13.8137, 2150.23, -27.6228]
     nc4=[-1280557, 7.95, -0.9646, 13.9836, 2292.44, -27.8623]
     ic5=[-1481583, 7.581, -0.9316, 13.6106, 2345.09, -40.2128]
     nc5=[-1524891, 7.331, -0.8914, 13.9778, 2554.6, -36.2529]
     nc6=[-1778901, 6.968, -0.8463, 14.0568, 2825.42, -42.7089]
     nc7=[-2013803, 6.529, -0.7954, 13.9008, 2932.72, -55.6356]
     nc9=[-255104, 5.693, -0.6782, 13.9548, 3290.56, -71.5056]

components=input("How many components are you gonna use? ")

g=[]
for i in range (components):
但是我被困在那里了。如何在不输入这么多代码的情况下填充g


我可能会使用4个组件,但它们可能不同。我怎样才能做更一般的事情?是用于正确的命令还是需要在while循环中执行?

据我所知,您需要选择一系列组件

和往常一样,在尝试编写泛型代码时,将它们放在数据结构中。我建议列出:

choices = [nc3, ic4, nc4, ic5, nc5, nc6, nc7, nc9]
现在问题变得非常简单:

n = input("How many components are you gonna use? ")
components = choices[0:n]
如果您希望按索引选择要使用的组件:

如果您希望按名称选择要使用的组件:


据我所知,您需要选择一系列组件

和往常一样,在尝试编写泛型代码时,将它们放在数据结构中。我建议列出:

choices = [nc3, ic4, nc4, ic5, nc5, nc6, nc7, nc9]
现在问题变得非常简单:

n = input("How many components are you gonna use? ")
components = choices[0:n]
如果您希望按索引选择要使用的组件:

如果您希望按名称选择要使用的组件:


谢谢你的帮助。在这种情况下,如何拾取不在0:n范围内的组件?假设我想要nc3、ic4和nc5,但不是nc4或ic5,我该怎么做?@JorgeReyes,一旦你有了一个列表,你就可以使用一系列值作为索引,或者使用离散的数字列表作为索引。您发布的代码表明您希望为索引使用一系列值。这个答案准确地回答了你的帖子。谢谢你的帮助。在这种情况下,如何拾取不在0:n范围内的组件?假设我想要nc3、ic4和nc5,但不是nc4或ic5,我该怎么做?@JorgeReyes,一旦你有了一个列表,你就可以使用一系列值作为索引,或者使用离散的数字列表作为索引。您发布的代码表明您希望为索引使用一系列值。这个答案准确地回答了你的帖子。