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
ladypxy
V2EX  ›  Python

将 list 中的元素分组并统计每组的个数

  •  
  •   ladypxy · 2019-11-15 13:31:35 +08:00 · 3761 次点击
    这是一个创建于 1595 天前的主题,其中的信息可能已经有所发展或是发生改变。

    list 是由数字组成,数字可能有重复。比如

    [-10, -1.67, 1.74, 27.43, -3.4, -0.59, 13.01, 18.69, 0.68, -1.17, 2.96, 13.14, 12.76, -0.14, 12.76, 3, 3, 5, 7, -2, 20]

    现在要求是传入参数 n,然后将 list 按照最大最小值分成 n 份,然后统计每份中的元素有多少个

    比如 n=5,那就分成

    -10 到 -4

    -4 到 2

    2 到 8

    8 到 14

    14 到 20

    然后统计这个范围内的元素个数

    请问怎么用最少的 for 循环来实现呢?

    14 条回复    2019-11-18 00:56:16 +08:00
    NerdTsai
        1
    NerdTsai  
       2019-11-15 13:45:55 +08:00
    除去内置方法中的 for 循环实现,,大概只需要 1 个 for 吧
    hakono
        2
    hakono  
       2019-11-15 13:53:53 +08:00 via iPhone
    数组先排个序,然后一个 for 循环遍历一遍不就行了。。。
    foamvalue
        3
    foamvalue  
       2019-11-15 13:54:15 +08:00
    已知最大最小值的情况下么,只要一个 for。
    fengmumu
        4
    fengmumu  
       2019-11-15 13:56:55 +08:00
    只能想到 先遍历一遍 获取最大最小数值,然后得出区间,再遍历一遍 归类放,循环两次
    wangyzj
        5
    wangyzj  
       2019-11-15 14:07:41 +08:00
    ```
    newlist = []
    templist = []
    num = len(mylist) // N
    j = 0
    for i in sorted(mylist):
    if j % num == 0 and j != 0:
    newlist.append(templist)
    templist = []
    if j % num < num:
    templist.append(i)
    j = j + 1

    print(newlist)
    ```
    alphatoad
        6
    alphatoad  
       2019-11-15 14:11:41 +08:00 via iPhone
    O(n)
    cherbim
        7
    cherbim  
       2019-11-15 14:24:08 +08:00 via iPhone
    先用 sorted 排序,然后一个 for 遍历……
    arloor
        8
    arloor  
       2019-11-15 15:09:38 +08:00 via Android
    java stream 可以做到不写 for
    xiaolinjia
        9
    xiaolinjia  
       2019-11-15 16:29:31 +08:00
    import math
    a = [-10, -1.67, 1.74, 27.43, -3.4, -0.59, 13.01, 18.69, 0.68, -1.17, 2.96, 13.14, 12.76, -0.14, 12.76, 3, 3, 5, 7, -2, 20]


    def function(temp: list, n):
    step = math.ceil(len(temp)/n)
    temp.sort()
    s = slice(0, step)
    for i in range(n):
    print(temp[s])
    s = slice(s.start + step, s.stop + step)

    if __name__ == '__main__':
    function(a, 6)
    TimePPT
        10
    TimePPT  
       2019-11-15 21:54:41 +08:00   ❤️ 3
    # 如果用 pandas 可以一个 for 都不用

    import pandas as pd

    lst = [-10, -1.67, 1.74, 27.43, -3.4, -0.59, 13.01, 18.69, 0.68, -1.17, 2.96, 13.14, 12.76, -0.14, 12.76, 3, 3, 5, 7, -2, 20]

    n = 5

    df_cut = pd.cut(lst, n)

    df_cut.value_counts()

    # pandas.cut 可以设置一系列函数确定均分区间的规则: https://pandas.pydata.org/pandas-docs/version/0.23.4/generated/pandas.cut.html
    ladypxy
        11
    ladypxy  
    OP
       2019-11-17 20:13:34 +08:00
    @TimePPT 感谢。请问怎么格式化 df_cut.value_counts()呢。这个输出的是类似于
    (-3,5] 8
    ( 5,13] 5
    这样的格式,怎么变成
    -3 5 8
    5 13 5
    ladypxy
        12
    ladypxy  
    OP
       2019-11-17 20:48:42 +08:00
    @wangyzj 这个写法只是把 list 的元素按照 N 来分成新的列表。而不是根据最大最小值来计算区间然后分配。而且还会把最大值忽略。。
    wangyzj
        13
    wangyzj  
       2019-11-18 00:51:15 +08:00
    @ladypxy 完,理解错你的意思了
    你要的是这个意思?
    mylist = sorted(mylist)
    newlist = []
    templist = []
    min = mylist[0]
    max = mylist[len(mylist)-1]
    delta = (max-min) / N
    next = min + delta
    j = 0
    for i in mylist:
    if i <= next:
    templist.append(i)
    if i >= next:
    newlist.append(templist)
    next = next+delta
    templist = []
    templist.append(i)
    j += 1
    if j == len(mylist):
    newlist.append(templist)

    print(newlist)
    wangyzj
        14
    wangyzj  
       2019-11-18 00:56:16 +08:00
    @ladypxy 完,理解错你的意思了
    你要的是这个意思?
    mylist = sorted(mylist)
    newlist = []
    templist = []
    min = mylist[0]
    max = mylist[len(mylist)-1]
    delta = (max-min) / (N-1)
    next = min + delta
    j = 0
    for i in mylist:
    if i <= next:
    templist.append(i)
    if i >= next:
    newlist.append(templist)
    next = next+delta
    templist = []
    templist.append(i)
    j += 1
    if j == len(mylist):
    newlist.append(templist)

    print(newlist)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1093 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 18:55 · PVG 02:55 · LAX 11:55 · JFK 14:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.