How to match a value of a list of numbers to item from a list of names in netlogo?

匆匆过客 提交于 2019-12-08 04:07:12

问题


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.

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".

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

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

Ideas?


回答1:


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.

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:

Extension exception: No value for 9 in table.

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

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.

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)

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"]]

Note that the zipping expression could also have be written:

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

...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.




回答2:


You need the item primitive to select from the list after matching on the other list. I am not sure what the precision line is for. However, here is a self contained piece of code that I think demonstrates what you want to do. Note that NetLogo counts positions from 0, not 1. I also used arbitrary numbers in the list so you don't get confused between the number in the list and its position.

to testme
  let usedstrategies (list "chicken" "duck" "monkey" "dog")
  let zq (list 5 6 7 8)
  let strategynum position 7 zq
  let thisstrategy item strategynum usedstrategies
  type "Selected strategy number " type strategynum
    type " which is " print thisstrategy
end


来源:https://stackoverflow.com/questions/49468584/how-to-match-a-value-of-a-list-of-numbers-to-item-from-a-list-of-names-in-netlog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!