Google 应用引擎 如何计算日期存储中的 SUM?
我想知道,如何获得从数据存储(python)中获得的评级实体的总和?
我应该:
ratingsum = 0
for rating in ratings:
ratingsum + rating
print ratingsum
?
Im wondering, how can i get a SUM of a rating entity i get from the datastore (python)?
should i:
ratingsum = 0
for rating in ratings:
ratingsum + rating
print ratingsum
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,差不多就是这样。检索您想要求和的所有实体,并在您的应用程序中对它们求和。 GQL 中没有
SUM
。如果您想要完成的任务是找到某个实体的平均评级,那么有更好的方法。
找到事物的平均评级是一个简单的查找,添加一个新的评级只是:
App Engine数据存储区的流行习惯是在写入时做尽可能多的工作,在读取时尽可能少做工作,因为会发生读取更频繁。
Yep, that's pretty much it. Retrieve all the entities you want to sum, and sum them in your app. There is no
SUM
in GQL.If what you're trying to accomplish is to find the average rating for an entity, there's a better way.
Finding the thing's average rating is a simple lookup, and adding a new rating is just:
The prevailing idiom of the App Engine datastore is to do as much work as you can on write, and as little as possible on read, since reads will happen much more often.
我无法回答您问题的 Google App Engine 部分,但您的代码(如果您将
+
更改为+=
)相当于:我很漂亮确保您可以在任何包含类似数字对象的序列或可迭代对象上使用
sum()
。I can't answer the Google App Engine part of your question, but your code (if you change the
+
to a+=
) is equivalent to:I'm pretty sure you can use
sum()
on any sequence or iterable containing number-like objects.