且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Netlogo:如何计算列表中列表项的总和?

更新时间:2022-12-09 18:34:38

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