列表中的值之和

huangapple go评论53阅读模式
英文:

Sum of values in a list

问题

reduce + 给出错误:'+' 预期输入为数字,但得到了列表。

如果我使用 sum 而不是 reduce +,则所有学生的结果都为 0。

如果我使用 map sum 而不是 reduce +,则会出现错误:'SUM 预期输入为列表,但得到了数字 0'。

英文:

I have students (turtles) and 30 schools (patches). For each student I have created a variable (utilities) from a utility function containing the utility of each school (so 30 values for each student). The problem is that I cannot generate a new variable that sums the 30 utility values for each student.

turtles-own
[
  
  utilities; schools' utilities for each individual
  cumul;
]
patches-own
[
  
  schoolachievement_2 ; overall mean school achievement
  
]
 to go
ask turtles [ utility ]

to utility
  set utilities [ school-utility ] of patches with [ schoolachievement_2 > 0 ]
  set cumul  reduce +  [ utilities ] of turtles
end 
end

reduce + gives the error: '+ expected input to be a number but got the list'.

If I use sum instead of reduce + I get 0 for all students.
If instead of reduce + I use map sum I get the error: 'SUM expected input to be a list but got the number 0 instead'.

答案1

得分: 1

LeirsW在语法问题上是正确的。但实际上这里存在一些问题。首先,我假设school-utility要么是一个修补(school)变量,要么是一个报告器,它使用其他修补变量返回一个数字。由于它似乎不知道是哪只乌龟调用了它,答案可能对所有乌龟都相同。这是否是你想要的,还是我漏掉了什么?接下来,在utility程序中,第一行让乌龟创建一个效用列表(如果没有修补满足with条件,则可能为空)。在下一行中,你让每只乌龟尝试对所有乌龟的效用求和,而不仅仅是自己的效用。因此,你正在对一个列表的列表求和,而不仅仅是一个简单的列表。有几种方法可以做到这一点。
第一种方法是

let cumul sum [sum utilities] of turtles

这使每只乌龟对其效用求和并将其传递给累积求和。
另一种方法是使用sentence将列表的列表折叠为一个简单的列表,然后对其求和。

let cumul sum sentence [utilities] of turtles

但是,这两种解决方案都提出了一个问题,即为什么每只乌龟都要执行相同的操作。cumul的计算应该在utility之外进行,以便仅执行一次。

当然,也许cumul只应该针对个别乌龟?如果是这样,你只需要

let cumul sum utilities
show cumul

这样你就得到了正确的结果。

Charles

英文:

LeirsW is correct about the syntax problem. But there are actually a number of issues here. First, I assume that school-utility is either a patch (school) variable or a reporter that uses other patch variables to return a number. Since it does not seem to know what turtle called it, the answer will likely be the same for all turtles. Is that what you want, or have I missed something? Next, in the utility routine, the first line has the turtle create a list of utilities (which could be empty if no patches satisfy the with condition). In the next line you have each turtle try to sum the utilities of all turtles, not just its own. Therefore, you are summing a list of lists, not just a simple list. There are a couple of ways you might do that.
The first is

let cumul sum [sum utilities] of turtles

This has each turtle sum its utilities and pass that to the culumlative summation.
Another is to collapse the list of lists to a simple list with sentence, then to sum it.

let cumul sum sentence [utilities] of turtles

But, both these solutions beg the question of why each turtle is being asked to do the same thing. The calculation of cumul should take place outside of utility so it is only done once.

Of course perhaps cumul should just be for the individual turtle? If so, you only need

let cumul sum utilities
show cumul

and you have it in the right place.

Charles

huangapple
  • 本文由 发表于 2023年6月8日 21:42:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定