Netlogo:从其他两个列表的关系中获取第三个列表
Netlogo: Obtaining a third list from the relation of other two lists
我想从前两个列表的关系中提取第三个列表。这是示例:
我在 NetLogo 中有一个输出 0 和 1 列表的进程,我们称它为 List_A:
let List_A [0 1 0 1 1 ]
0 和 1 的位置总是根据与此问题无关的几个因素而变化。
还有一个与 List_A 相关的列表,名为 List_B:
let List_B [“residential” “industrial” “commercial” “farmland” “preservation”]
此列表项的位置从不改变。
我要获取的第三个列表是 List_B 的项目,其位置对应于 List_A 的项目中值为 1 的位置。因此,根据前面的示例,这将是一个由[“工业”“农田”“保护”]组成的列表,因为“住宅”和“商业”的位置对应于值 0,因此从列表中删除。
使用此代码取得了一些进展,该代码输出值为 1 的项目 List_A 的位置列表:
to-report comp-positions
report filter [ i -> item i List_A = 1 ]
n-values (length List_A) [ i -> i ]
end
但不知道如何应用到List_B得到第三个列表
可能有 one-step 方法,但也许可以使用 map
和 filter
的组合?使用 ifelse-value
的 map
可以生成零和 "List_B" 项的列表,然后过滤以删除零:
to filter-map
let List_A [0 1 0 1 1 ]
let List_B [ "residential" "industrial" "commercial" "farmland" "preservation"]
print filter [ i -> i != 0 ] ( map [ [ a b ] -> ifelse-value (a = 1) [b] [0] ] List_A List_B)
end
为了完整起见,还有三种方式:
print map last filter [ p -> first p = 1 ] (map list List_A List_B)
print reduce sentence (map [ [a b] -> item a (list [] b) ] List_A List_B)
print reduce [ [acc val] ->
ifelse-value (first val = 1) [ lput last val acc ] [ acc ]
] fput [] (map list List_A List_B)
我想从前两个列表的关系中提取第三个列表。这是示例:
我在 NetLogo 中有一个输出 0 和 1 列表的进程,我们称它为 List_A:
let List_A [0 1 0 1 1 ]
0 和 1 的位置总是根据与此问题无关的几个因素而变化。
还有一个与 List_A 相关的列表,名为 List_B:
let List_B [“residential” “industrial” “commercial” “farmland” “preservation”]
此列表项的位置从不改变。
我要获取的第三个列表是 List_B 的项目,其位置对应于 List_A 的项目中值为 1 的位置。因此,根据前面的示例,这将是一个由[“工业”“农田”“保护”]组成的列表,因为“住宅”和“商业”的位置对应于值 0,因此从列表中删除。
使用此代码取得了一些进展,该代码输出值为 1 的项目 List_A 的位置列表:
to-report comp-positions
report filter [ i -> item i List_A = 1 ]
n-values (length List_A) [ i -> i ]
end
但不知道如何应用到List_B得到第三个列表
可能有 one-step 方法,但也许可以使用 map
和 filter
的组合?使用 ifelse-value
的 map
可以生成零和 "List_B" 项的列表,然后过滤以删除零:
to filter-map
let List_A [0 1 0 1 1 ]
let List_B [ "residential" "industrial" "commercial" "farmland" "preservation"]
print filter [ i -> i != 0 ] ( map [ [ a b ] -> ifelse-value (a = 1) [b] [0] ] List_A List_B)
end
为了完整起见,还有三种方式:
print map last filter [ p -> first p = 1 ] (map list List_A List_B)
print reduce sentence (map [ [a b] -> item a (list [] b) ] List_A List_B)
print reduce [ [acc val] ->
ifelse-value (first val = 1) [ lput last val acc ] [ acc ]
] fput [] (map list List_A List_B)