如何建立清单清单

时间:2019-04-08 14:25:27

标签: netlogo

所以我想建立一个看起来像这样的列表x:[[a1 b1][a2 b2][a3 b3]...]。 A和b分别通过one-of listAone-of listB选择。我找不到如何轻松创建列表的方法,不能只是将项目添加到空列表中?

2 个答案:

答案 0 :(得分:1)

检出lput的字典条目以获取添加到列表的一般语法,并查看the programming guide entrythis answer以获得更多信息。对于这个特定问题,请看下面的示例:

to build-lists
  ca
  let a [ 1 2 3 4 5 ]
  let b [ "a" "b" "c" "d" "e" ]

  ; Unordered version:
  let ab []
  repeat length a [
    set ab lput ( list one-of a one-of b ) ab
  ]
  print "Randomly sampled list:"
  print ab

  ; Ordered version:
  set ab ( map [ [ i j ] -> list i j ] a b )
  print "Ordered list: "
  print ab  
  reset-ticks
end

哪个输出类似:

Randomly sampled list:
[[2 a] [2 c] [1 d] [4 d] [1 e]]
Ordered list: 
[[1 a] [2 b] [3 c] [4 d] [5 e]]

答案 1 :(得分:0)

分别使用n-valuesconcise syntax for anonymous procedures,对Luke C的答案略有不同:

let a [ 1 2 3 4 5 ]
let b [ "a" "b" "c" "d" "e" ]

; Unordered version:
print n-values length a [ list one-of a one-of b ]

; Ordered version:
print (map list a b)