如何将数字列表的值与netlogo中名称列表中的项匹配? [英] How to match a value of a list of numbers to item from a list of names in netlogo?

查看:117
本文介绍了如何将数字列表的值与netlogo中名称列表中的项匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试某些操作(在netlogo中),但是它不起作用.我想要数字列表中的头寸值.而且我想使用其中的数字从名称列表中检索名称.

I am trying something (in netlogo), but it is not working. I want a value of a position from a list of numbers. And I want to use the number that comes out of it to retrieve a name from a list of names.

因此,如果我有一个像[1 2 3 4]这样的列表,请用[鸡肉"鸭子"猴子"狗"]列出一个列表 我希望我的数字2与鸭子"相对应.

So if I have a list like [1 2 3 4] en a list with ["chicken" "duck" "monkey" "dog"] I want my number 2 to correspond with "duck".

到目前为止,我的zq是一个数字列表,我的使用策略是一个名称列表.

So far, my zq is a list of numbers and my usedstrategies is a list of names.

let m precision (max zq) 1
let l position m zq
let p (position l zq) usedstrategies

但是当我尝试这样做时,结果将是错误的,因为l并不是旧策略的一部分.

But when I try this the result will be false, because l is not part of usedstrategies.

想法?

推荐答案

Jen的解决方案很好,但是我认为这对于

Jen's solution is perfectly fine, but I think this could also be a good use case for the table extension. Here is an example:

extensions [table]

to demo

  let usedstrategies ["chicken" "duck" "monkey" "dog"]
  let zq [5 6 7 8]  
  let strategies table:from-list (map list zq usedstrategies)  

  ; get item corresponding with number 7:
  print table:get strategies 7

end

这里的表"是一种数据结构,其中一组键与值相关联.在这里,数字是关键,策略是价值.

A "table", here, is a data structure where a set of keys are associated with values. Here, your numbers are the keys and the strategies are the values.

如果您尝试获取表中没有键的项目(例如table:get strategies 9),则会出现以下错误:

If you try to get an item for which there is no key in the table (e.g., table:get strategies 9), you'll get the following error:

扩展例外:表中没有9的值.

Extension exception: No value for 9 in table.

这里有一些有关代码工作原理的详细信息.

Here is a bit more detail about how the code works.

要构建表,我们使用table:from-list报告程序,该报告程序将列表列表作为输入,并返回给您一个表,其中每个子列表的第一项用作键,第二项用作键.值.

To construct the table, we use the table:from-list reporter, which takes a list of lists as input and gives you back a table where the first item of each sublist is used as a key and the second item is used as a value.

要构建列表列表,请使用 map 原始的.这部分要理解起来有些棘手. map原语需要两种输入:一个或多个列表,以及要应用于这些列表元素的报告器.报告者排在第一位,整个表达式需要放在括号内:

To construct our list of lists, we use the map primitive. This part is a bit more tricky to understand. The map primitive needs two kind of inputs: one or more lists, and a reporter to be applied to elements of these lists. The reporter comes first, and the whole expression needs to be inside parentheses:

(map list zq usedstrategies)

此表达式将两个列表压缩"在一起:它使用zq的第一个元素和usedstrategies的第一个元素,并将它们传递到

This expression "zips" our two lists together: it takes the first element of zq and the first element of usedstrategies, passes them to the list reporter, which constructs a list with these two elements, and adds that result to a new list. It then takes the second element of zq and the second element of usedstrategies and does the same thing with them, until we have a list that looks like:

[[5 "chicken"] [6 "duck"] [7 "monkey"] [8 "dog"]]

请注意,也可以将zip表达式写成:

Note that the zipping expression could also have be written:

(map [ [a b] -> list a b ] zq usedstrategies)

...但是这是一种更round回的方式. list报告程序本身已经是我们想要的.无需构造一个单独的匿名报告程序来完成相同的任务.

...but it's a more roundabout way to do it. The list reporter by itself is already what we want; there is no need to construct a separate anonymous reporter that does the same thing.

这篇关于如何将数字列表的值与netlogo中名称列表中的项匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆