简体   繁体   English

在Python中使用嵌套的for循环处理多维列表?

[英]Processing multidimensional lists with nested for loops in python?

I am writing code for Nuke - The foundry.. I have been banging my head for 3 hours now.. looking up on google but without success What i am trying to do is basically create 2 dimensional list and populate it with something.. In my case i want to populate the list with db = [['nodeName1', 'inputnName1'], ['nodeName2', 'imputName2'], etc...] 我正在为Nuke-The Foundry编写代码。.我已经敲了三个小时了..在Google上查找但没有成功,我要尝试的工作基本上是创建二维列表并在其中填充一些内容。我的情况下,我想用db = [['nodeName1', 'inputnName1'], ['nodeName2', 'imputName2'], etc...]填充列表db = [['nodeName1', 'inputnName1'], ['nodeName2', 'imputName2'], etc...]

I create the list with db = [[None for x in range(2)] for y in range (nMasks)] - where nMasks is a variable of how many rows there should be in "db" 我用db = [[None for x in range(2)] for y in range (nMasks)]创建列表-其中nMasks是“ db”中应有多少行的变量

now i want to populate the list with my variables somehow.. i tried this: 现在我想以某种方式用我的变量填充列表。

for i in range(len(db)): #row
    for j in range(len(db[i])): #element
        for n in nuke.selectedNodes():
            if j == 0:
                db[i][j] = n #populate list with node
            if j != 0:
                db[i][j] = 'a' #for now it's just an a and nothing more but ill have to change this to an actual nodeName 

This gives me different result of what i want - when i do: 这给了我想要的结果不同-当我这样做时:

print db[0][0]['name'].value()
print db[0][1]

print db[1][0]['name'].value()
print db[1][1]

i get result: 我得到结果:

Result: 结果:

Node1 a Node1 a 节点1 a节点1 a

and i want it to look like: 我希望它看起来像:

Result: 结果:

Node1 a Node2 a 节点1 a节点2 a

note: maybe there is even more elegant solution for this? 注意:也许对此有更优雅的解决方案?

I normally populate a list by appending values to it. 我通常通过将值附加到列表中来填充列表。 This way you don't need to know in advance the size of the list. 这样,您无需事先知道列表的大小。 What it seems that you are trying to do from your desired output, is to get the selectedNodes into a 2D array db . 您似乎想要从所需的输出中执行的操作是将selectedNodes放入2D数组db It seems like db[i][0] should be be nuke.selectedNodes()[i] while db[i][1] should be a string. 似乎db[i][0]应该是nuke.selectedNodes()[i]db[i][1]应该是字符串。 I don't really understand what your nMasks has to do with the number of selected nodes but, if your intention was to get all the selected nodes, it seems that the following would be more natural to get the 2D list that you want 我不太了解您的nMasks与所选节点的数量有什么关系,但是,如果您打算获取所有所选节点,那么以下似乎更自然的获取您想要的2D列表

sn = nuke.selectedNodes()
db = 
for n in nuke.selectedNodes():
    db.append([n,'a'])

edit 编辑

There are many, many ways to get the same list. 获得相同列表的方法有很多。 For instance, you could use list comprehension to sum it up into a single line 例如,您可以使用列表推导将其汇总为一行

db = [[n,'a'] for n in nuke.selectedNodes()]

But if you want to use nested loops like in your question's code, you could do it like this 但是,如果您想在问题的代码中使用嵌套循环,可以这样做

db = [[None for x in range(2)] for y in range (nMasks)]
for i in range(len(db)):
    for j in range(len(db[0])):
        if j == 0:
            db[i][j] = nuke.selectedNodes()[i] #populate list with node
        else:
            db[i][j] = 'a'

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

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