Python 如何根据一个列表中的阈值对另一个列表中的值进行分类?
我想根据另一个列表中的阈值对一个列表中的值进行分类。换句话说,我想逐个比较列表项和列表中的阈值,然后得到类别的输出列表Python 如何根据一个列表中的阈值对另一个列表中的值进行分类?,python,list,sorting,list-comprehension,Python,List,Sorting,List Comprehension,我想根据另一个列表中的阈值对一个列表中的值进行分类。换句话说,我想逐个比较列表项和列表中的阈值,然后得到类别的输出列表 input: Values = [9999, 3000, 400, 9999, 1000] - it is variably long due to input data Threshold = [10000, 5000, 1500, 800, 0] - also will be changed, so have to be variable - but always sort
input:
Values = [9999, 3000, 400, 9999, 1000] - it is variably long due to input data
Threshold = [10000, 5000, 1500, 800, 0] - also will be changed, so have to be variable - but always sorted descending to 0
Expected output (categories' numbers should be based on index):
cat = [0,1,3,0,2]
我相信这可能是通过高级列表理解来完成的,而我对此不太熟悉。所以我试过:
val_cat = []
thres_len = len(Threshold)
for item in Values:
for vis in range(0,thres_len - 1):
if Threshold[vis+1] < int(item) <= Threshold[vis]:
val_cat = val_cat + [vis]
else:
pass
val_cat=[]
thres_len=len(阈值)
对于值中的项目:
对于范围内的可见光(0,thres_len-1):
如果阈值[vis+1]
cat = [next(i-1 for i,t in enumerate(Threshold) if t <= v) for v in Values]
cat=[next(i-1表示枚举中的i,t)(阈值)如果t