[英]Finding the distribution of a field in a list of objects?
I have a list of objects. 我有一个对象列表。 Each object has a field called grade
whose value is between 0 and 5. Now I want to see the distribution of this field across my list of objects. 每个对象都有一个称为grade
的字段,其值在0到5之间。现在,我想查看该字段在对象列表中的分布。 Is there any way to find it? 有什么办法找到它吗?
I know I can iterate over the whole objects and find it out but I don't want to do that. 我知道我可以遍历整个对象并找出来,但是我不想这样做。
As near as I can tell, using a table Table
with a grade
column you need something like this: 据我所知,使用带grade
列的表格Table
需要以下内容:
counts = Table.objects.annotate(count=Count("grade", distinct=True)
This adds a count
attribute to each member of the counts
query set, which you can access just like a regular database column. 这会将count
属性添加到counts
查询集的每个成员中,您可以像访问常规数据库列一样访问它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.