第一种(把元素放进篮子里)
from random import randint
from pprint import pprint
score_list = [63, 67, 73, 84, 88, 97, 70, 85, 68, 96, 95, 60, 83, 70, 77, 86, 83, 94, 100, 82]
print(score_list)
low = []
mid = []
ok = []
def deal_with(v):
global low
global mid
global ok
if 50< v <70:
low.append(v)
return
elif 70<=v<90:
mid.append(v)
return
elif 90<=v<=100:
ok.append(v)
return
map(deal_with, score_list)
pprint({
"low":low,"mid":mid,"ok":ok})
第二种(把元素放进篮子里)
from random import randint
from pprint import pprint
from itertools import groupby
score_list = [63, 67, 73, 84, 88, 97, 70, 85, 68, 96, 95, 60, 83, 70, 77, 86, 83, 94, 100, 82]
print(score_list)
data1 = sorted(score_list)
print(data1)
def deal_with_simple_data(d):
if 50 < d < 70:
return "low"
elif 70 <= d < 90:
return "mid"
elif 90 <= d <= 100:
return "ok"
itor_data = groupby(data1, key=deal_with_simple_data)
pprint([(name, list(eles)) for name,eles in itor_data])
第三种(每个元素属于哪种篮子)
1.
import pandas as pd
score_list = [63, 67, 73, 84, 88, 97, 70, 85, 68, 96, 95, 60, 83, 70, 77, 86, 83, 94, 100, 82]
print(score_list)
bins = [50,70,90,100]
res = pd.cut(score_list, bins)
res1 = pd.cut(score_list, bins, labels=["及格","中等","优秀"])
print(res)
print("---"*35)
print(res1)
"""
[63, 67, 73, 84, 88, 97, 70, 85, 68, 96, 95, 60, 83, 70, 77, 86, 83, 94, 100, 82]
[(50, 70], (50, 70], (70, 90], (70, 90], (70, 90], ..., (70, 90], (70, 90], (90, 100], (90, 100], (70, 90]]
Length: 20
Categories (3, interval[int64]): [(50, 70] < (70, 90] < (90, 100]]
---------------------------------------------------------------------------------------------------------
[及格, 及格, 中等, 中等, 中等, ..., 中等, 中等, 优秀, 优秀, 中等]
Length: 20
Categories (3, object): [及格 < 中等 < 优秀]
"""
2.