How to remove several items from a unsorted list in Netlogo

a 夏天 提交于 2019-12-24 01:16:44

问题


so i'm a bit struggling with Lists in Netlogo, so basically i've two lists and i want to remove the items that are in List 1 from List 2, for example:

List 1 : [8 6 9 7 1 3]

List 2: [5 9 8]

Resulting List : [6 7 1 3]

I've tried the following code but it returns an empty list:

if List 2 != []
   [ 
          foreach List 2 
          [
             let p position ? List 1 
             if p = true
                [
                  set List 1 remove-item p List 1
                ]
          ]
    ]

Any ideas ?


回答1:


A combination of member? and filter will get you there:

let list1 [8 6 9 7 1 3]
let list2 [5 9 8]
let result filter [ x -> not member? x list2 ] list1
print result

Will print the desired:

[6 7 1 3]

Tip: whenever you find yourself trying to use an index for anything in NetLogo, you are probably not doing things in the optimal way. NetLogo has tons of functions (like filter, in this case) that operate on lists as a whole. There is rarely a need to explicitly loop through them.



来源:https://stackoverflow.com/questions/49379498/how-to-remove-several-items-from-a-unsorted-list-in-netlogo

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