Python 统计数据列表分布

# 左闭右开
def get_scope_percentage(m_list, m_start_num, m_end_num, m_base):
    if not m_list:
        return [['%s <= x <= %s' % (m_start_num, m_end_num), m_start_num, m_end_num, 0, 0, 0]]
    m_scope_list = []
    m_count = int((m_end_num - m_start_num) / m_base) + 1
    for m_i in range(m_count):
        m_start = m_start_num + m_base * m_i
        m_end = m_start + m_base
        if m_end < m_end_num:
            m_scope_list.append(['%s <= x < %s' % (m_start, m_end), m_start, m_end, len(m_list), 0])
        else:
            m_end = m_end_num
            m_scope_list.append(['%s <= x <= %s' % (m_start, m_end), m_start, m_end, len(m_list), 0])
            break

    for m_i in range(len(m_scope_list)):
        m_start = m_scope_list[m_i][1]
        m_end = m_scope_list[m_i][2]
        for m_num in m_list:
            if m_start <= m_num < m_end:
                m_scope_list[m_i][-1] += 1
            # 末尾极限值判断
            if m_end == m_end_num and m_num == m_end_num:
                m_scope_list[m_i][-1] += 1

    for m_i in range(len(m_scope_list)):
        m_scope_list[m_i].append(float(m_scope_list[m_i][-1]) / m_scope_list[m_i][-2])

    return m_scope_list

你可能感兴趣的:(Python)