推荐学习书目
› 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
lixia625
V2EX  ›  Python

问个代码问题

  •  
  •   lixia625 · Oct 24, 2014 · 3646 views
    This topic created in 4361 days ago, the information mentioned may be changed or developed.
    现有一个整型数的list,a=[1,1,5,6],大小不一,如果用Counter(a),可以统计出各个数出现的频率,1:2,5:1,6:1
    现在的问题是:如何统计某个区间段内a中整型数的个数;
    如
    1-4:2,5-10:2

    有没有一些比较pythonic的做法
    还是只能自己按逻辑来写
    6 replies  •  2014-10-25 01:39:02 +08:00
    andychen20121130
        1
    andychen20121130  
       Oct 24, 2014
    自己写。
    bcxx
        2
    bcxx  
       Oct 24, 2014
    可以先做一个 groupby 再 counter
    cxe2v
        3
    cxe2v  
       Oct 24, 2014
    你去除某个区间的数到一个临时LIST,再用Counter(a)统计不就可以了?
    flyee
        4
    flyee  
       Oct 24, 2014 via iPhone
    排序+二分
    catbaron
        5
    catbaron  
       Oct 24, 2014
    def Fltr(down,up,arr):
    return len(filter(lambda x:(x>=down and x <= up),arr))

    _(:з」∠)_
    lixia625
        6
    lixia625  
    OP
       Oct 25, 2014
    本来是想处理了数据画直方图,结果发现强大的matplotlib直接可以画都不用处理ORZ
    不过还是谢谢上面回复的朋友。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2311 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 08:43 · PVG 16:43 · LAX 01:43 · JFK 04:43
    ♥ Do have faith in what you're doing.