Multi-conditional classification count of List in Python

    list0 = [] -sharp 0
    list1 = [] -sharp 1
    list2 = [] -sharp 2-5
    list3 = [] -sharp 6-10
    list4 = [] -sharp 10-100
    list5 = [] -sharp 100
    for u in users_list:
        if (u["balance"]/100) <= 0:
            list0.append(u)
        if 0 < (u["balance"]/100) <= 1:
            list1.append(u)
        if 2 < (u["balance"]/100) <= 5:
            list2.append(u)
        if 6 < (u["balance"]/100) <= 10:
            list3.append(u)
        if 10 < (u["balance"]/100) <= 100:
            list4.append(u)
        if 100 < (u["balance"]/100):
            list5.append(u)
    print(str(len(list0)),str(len(list1)),str(len(list2))....)

is there a better way to classify it without creating an empty list and count the total number of each condition

Jun.24,2021

can be used in a dictionary, and each condition is saved as a value


is there any expression like switch or case in python

?

this is the official document python.org/2/faq/design.html-sharpwhy-isn-t-there-a-switch-or-case-statement-in-python" rel=" nofollow noreferrer "> https://docs.python.org/2/faq., recommended by @ chjian92 in dictionary form


use the cut function of pandas

give an example:

import pandas as pd
import numpy as np

a = [-100, 100, 500, 1000, 9001, 10001]
bins = [-np.inf, 0, 1, 5, 10, 100, np.inf]

res = pd.Series(a).groupby(
        pd.cut(np.array(a)/100, 
               bins=bins,
               labels=[
                       '0',
                       '1',
                       '2-5',
                       '6-10',
                       '10-100',
                       '100'
                       ],
               )
        ).count()
print(res)

result:

0       1
1       1
2-5       1
6-10      1
10-100    1
100     1
dtype: int64
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b373a0-2c05b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b373a0-2c05b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?