V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
zhuyw2006
V2EX  ›  Python

新手求助, Django 去重并统计数量的问题!

  •  
  •   zhuyw2006 · 2018-02-23 00:50:08 +08:00 via iPhone · 5857 次点击
    这是一个创建于 2225 天前的主题,其中的信息可能已经有所发展或是发生改变。
    数据库全部读取,显示都没问题,怎么把重复的合并.,并在他们每个后面统计出数量。数据库里面就一列,里面有很多重复名称。
    10 条回复    2018-02-28 11:28:56 +08:00
    wellsc
        1
    wellsc  
       2018-02-23 00:54:22 +08:00
    set or sql distinct
    zhuyw2006
        2
    zhuyw2006  
    OP
       2018-02-23 01:18:02 +08:00 via iPhone
    @wellsc 去除重复是没问题,就是不知道如何统计显示出每个名称重复的数量
    Comphuse
        3
    Comphuse  
       2018-02-23 01:28:02 +08:00   ❤️ 1
    @zhuyw2006 import collections.Counter
    huntzhan
        4
    huntzhan  
       2018-02-23 01:28:54 +08:00
    group by xxx count xxx
    NaVient
        5
    NaVient  
       2018-02-23 09:27:28 +08:00
    先 xxx.objects.filter().distinct().values()将结果取为字典
    再 from collections import Counter 具体用法看看文档
    wizardoz
        6
    wizardoz  
       2018-02-23 09:51:20 +08:00
    楼主要的是不是这种?
    >>> from django.db.models import Count
    >>> pubs = Publisher.objects.annotate(num_books=Count('book'))
    >>> pubs
    <QuerySet [<Publisher: BaloneyPress>, <Publisher: SalamiPress>, ...]>
    >>> pubs[0].num_books
    73


    https://docs.djangoproject.com/en/2.0/topics/db/aggregation/
    xpresslink
        7
    xpresslink  
       2018-02-23 23:37:16 +08:00
    假设有这样一个只有一字段的 Model
    class MyModel(models.Model):
    □□□□item = models.CharField(max_length=100)
    □□□□def __str__(self):s
    □□□□□□□□return self.item
    □□□□class Meta:
    □□□□□□□□verbose_name = 'MM'

    >>> from temp.models import MyModel as MM
    MM.objects.values_list('item', flat=True)
    <QuerySet ['a', 'b', 'c', 'd', 'e', 'a', 'a', 'e', 'b', 'b', 'b', 'b', 'f', 'a', 'c', 'b']>
    >>> from django.db.models import Count
    >>> MM.objects.values_list('item', flat=True).annotate(Count('item'))
    <QuerySet ['c', 2, 'b', 6, 'a', 4, 'f', 1, 'e', 2, 'd', 1]>

    >>> from collections import Counter
    >>> Counter(MM.objects.values_list('item', flat=True))
    Counter({'b': 6, 'a': 4, 'c': 2, 'e': 2, 'd': 1, 'f': 1})
    xpresslink
        8
    xpresslink  
       2018-02-23 23:40:02 +08:00
    >>> MM.objects.values_list('item').annotate(Count('item'))
    <QuerySet [('c', 2), ('b', 6), ('a', 4), ('f', 1), ('e', 2), ('d', 1)]>
    zhuyw2006
        9
    zhuyw2006  
    OP
       2018-02-24 16:39:54 +08:00 via iPhone
    @Comphuse
    @huntzhan
    @NaVient
    @wizardoz
    @xpresslink 谢谢🙏,已经安装这样搞定了
    chengxiao
        10
    chengxiao  
       2018-02-28 11:28:56 +08:00
    @zhuyw2006 怎么搞定的 能给一下思路吗?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3137 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 13:01 · PVG 21:01 · LAX 06:01 · JFK 09:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.