我需要从列表中获取每个元素的总和
I need to get the sum of each element from a list
我有一个这样的列表:
[[0 1 2] [4 6 9] ... [-1 0 3]]
我需要得到 [3 19 ... 2]
,我的意思是第一个元素、第二个元素和 .... n - 元素的总和。
非常感谢你的帮助。
更新:我试过了:
to sum-first-item
let out []
ask turtles[
let sum-elem sum map first data-train
set out lput sum-elem out
]
end
可以使用 item
(see here, but also know about first
and last
) or as the current element of foreach
(see here).
访问每个列表的项目(在您的例子中:每个内部列表)
这里有很多方法可以帮助您实现目标。首先简单展示如何对每个内部列表进行操作,然后展示如何直接构建一个包含每个内部列表总和的列表:
to play-with-lists
print "The original list:"
let list-of-lists [[0 1 2] [4 6 9] [-1 0 3]]
print list-of-lists
print "Manually reporting sums:"
print sum item 0 list-of-lists
print sum item 1 list-of-lists
print sum item 2 list-of-lists
print "Building a list of sums with while loop using 'while':"
let list-of-sums []
let i 0
while [i < length list-of-lists] [
set list-of-sums lput (sum item i list-of-lists) (list-of-sums)
set i i + 1
]
print list-of-sums
print "Building a list of sums with for loop using 'foreach':"
set list-of-sums []
foreach list-of-lists [
inner-list ->
set list-of-sums lput (sum inner-list) (list-of-sums)
]
print list-of-sums
print "Building a list of sums using 'map':"
print map sum list-of-lists
end
编辑: Matteo 的 map sum the-list
版本是更简单的解决方案,所以我建议坚持使用。尽管如此,当您需要进行比求和更复杂的计算时,了解 reduce for 还是很有用的。
Netlogo 提供了一些非常强大的原语来处理列表。我看到您已经使用 map
,它为列表的每个项目运行一个报告器并报告结果列表。
另一个是 reduce
,它通过依次对列表中的所有项目应用报告程序将其组合成一个值。我建议阅读字典中关于 reduce 的内容并稍微尝试一下,因为它的工作原理并不是很明显 (https://ccl.northwestern.edu/netlogo/docs/dictionary.html#reduce)。
将这两者结合起来得到这段优雅的代码:
to try
let the-list[[0 1 2] [4 6 9] [-1 0 3]]
show map [ x -> reduce + x] the-list
;observer: [3 19 2]
end
我有一个这样的列表:
[[0 1 2] [4 6 9] ... [-1 0 3]]
我需要得到 [3 19 ... 2]
,我的意思是第一个元素、第二个元素和 .... n - 元素的总和。
非常感谢你的帮助。
更新:我试过了:
to sum-first-item
let out []
ask turtles[
let sum-elem sum map first data-train
set out lput sum-elem out
]
end
可以使用 item
(see here, but also know about first
and last
) or as the current element of foreach
(see here).
这里有很多方法可以帮助您实现目标。首先简单展示如何对每个内部列表进行操作,然后展示如何直接构建一个包含每个内部列表总和的列表:
to play-with-lists
print "The original list:"
let list-of-lists [[0 1 2] [4 6 9] [-1 0 3]]
print list-of-lists
print "Manually reporting sums:"
print sum item 0 list-of-lists
print sum item 1 list-of-lists
print sum item 2 list-of-lists
print "Building a list of sums with while loop using 'while':"
let list-of-sums []
let i 0
while [i < length list-of-lists] [
set list-of-sums lput (sum item i list-of-lists) (list-of-sums)
set i i + 1
]
print list-of-sums
print "Building a list of sums with for loop using 'foreach':"
set list-of-sums []
foreach list-of-lists [
inner-list ->
set list-of-sums lput (sum inner-list) (list-of-sums)
]
print list-of-sums
print "Building a list of sums using 'map':"
print map sum list-of-lists
end
编辑: Matteo 的 map sum the-list
版本是更简单的解决方案,所以我建议坚持使用。尽管如此,当您需要进行比求和更复杂的计算时,了解 reduce for 还是很有用的。
Netlogo 提供了一些非常强大的原语来处理列表。我看到您已经使用 map
,它为列表的每个项目运行一个报告器并报告结果列表。
另一个是 reduce
,它通过依次对列表中的所有项目应用报告程序将其组合成一个值。我建议阅读字典中关于 reduce 的内容并稍微尝试一下,因为它的工作原理并不是很明显 (https://ccl.northwestern.edu/netlogo/docs/dictionary.html#reduce)。
将这两者结合起来得到这段优雅的代码:
to try
let the-list[[0 1 2] [4 6 9] [-1 0 3]]
show map [ x -> reduce + x] the-list
;observer: [3 19 2]
end