带有计数器的列表迭代中的 Netlogo 列表

Netlogo list in list iterations with counter

我从 c​​sv 中读取了一个名为 fileList [[id, id2, id3],[10,10,11]] 的列表列表 但我有一个问题,我想遍历列表并在每次迭代中创建一个包含 id1、id3(不是 Id2)作为变量的海龟。我在 python 语法中的想法(我需要帮助将其转换为 NetLogo):

for x, list in enumerate(fileList):
       if x==0: #first list is names so I transpose the names to places in
           index_id=list.index(id) 
           index_id3=list.index(id3)
       else:
           create-turtle_nr1 #not in python syntax but the idea is to create turte to assign variables from list below  
           ask turtle_nr1 [set id1 item (item as list[index_id])]

总输出是三只海龟,变量为 id 和 id3。

在这种情况下,您应该能够使用 item 迭代地索引您的列表。本质上,对于每只乌龟,您希望它从列表列表中索引适当的 list-of-variables,然后从 that 列表中索引适当的变量。您可以从以下内容开始:

turtles-own [
   id
   id2
   id3
]

to list-of-lists

  ;;; these lists are just placeholders, of course, use your real list of lists
  ;;; as the "ids_list" variable in this case
  let id1list [ 1 2 3]    
  let id2list [ 44 55 66 ]
  let id3list [ "a" "b" "c" ]
  let ids_list ( list id1list id2list id3list )

  let n 0

  while [ n < 3 ] [  ;;; or however many turtles you end up wanting, 
                     ;;; as long as you have list variables for them 
    create-turtles 1 [
      set id item n (item 0 ids_list)
      set id3 item n (item 2 ids_list)
    ]
    set n n + 1
  ]

end

此过程创建了三只海龟,id 分别为 1、2 和 3,id2 为 0,id3 为 a、b 和 c。