Netlogo:如何计算列表中列表项的总和? [英] Netlogo: How to compute sum of items of lists within a list?

查看:132
本文介绍了Netlogo:如何计算列表中列表项的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将其总和=一种植物从其他植物(堂)获得的花粉总量,并将其存储在列表的列表中(每只海龟拥有的=植物). 以下代码会导致错误(计算总和时):

I would like to make the sum = the total of pollen recieved by a plant from other plants (Donnors) which is stored in a list of a list (own by each turtle = plant). The following code make an error (when computing the sum):

OF个预期输入是业务代表或业务代表集,但获得了清单 [[119.05593 50 50] [301.25853 50 50] [30.23906 50 50] [460.525845 50 50] [55.16717 50 50] [301.25853 50 50]].

OF expected input to be an agent or agentset but got the list [[119.05593 50 50] [301.25853 50 50] [30.23906 50 50] [460.525845 50 50] [55.16717 50 50] [301.25853 50 50]] instead.

有人可以帮我解决"set Tot_pol sum ..."行中的错误吗? 非常感谢您的帮助.

Does any one could help me about the mistake in the line "set Tot_pol sum ..." ? Many thanks for your help.

to check-pol [m]                     ;; we check the pollen recieved by the two morphs
set Donnors []                     ;; empty list of pollen donnors
ask zsps with [morph = m]          ;; morph of the pollen reciever
    [
    set totpol 0
    ;; check for pollen donnors and morph for compatiblity within a radius :
    ask zsps with[distance myself <= 20 and morph != m]
        [
        set totpol  (NMaleFlowers * 100 * item round (distance myself) pollination-list)  ;; the farther the less pollen
         set Donnors lput [ (list totpol NMaleFlowers NFemFlowers)] of myself Donnors
      ]

      set Tot_pol sum [ item (position 0 Donnors) Donnors ] of Donnors ;; total of pollen recieved
    ]
end

推荐答案

Luke的回答很好,应该可以解决您的问题.但是,我怀疑您将要进行大量此类总和.您可能希望设置一个to-report,您可以将其用于要求和的任何项目,只需传递项目编号和列表列表的名称即可.看起来像这样:

Luke's answer is good and should fix your problem. I suspect, however, that you are going to be doing lots of these types of sums. You may wish to set up a to-report that you can use for whichever item you want to sum over, just by passing the item number and the name of the list of lists. It would look like this:

to-report sum-item [#pos #listoflists ]
  let items map [ x -> item #pos x ] #listoflists
  report reduce [ [a b] -> a + b] items
end

第一行将相关项(从0记住索引)提取到新列表中,第二行将其相加.

The first line extracts the relevant item (remember index from 0) into a new list which the second line sums.

然后将其与set Tot_pol sum-item 0 Donnors

这篇关于Netlogo:如何计算列表中列表项的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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