#coding:utf-8
data_list=[
[‘ch1’,“ce1”,“lun1”,“TU1”],
[‘ch0’,“ce1”,“lun1”,“TU1”],
[‘ch1’,“ce1”,“lun1”,“TU2”],
[‘ch1’,“ce1”,“lun1”,“TU1”],
[‘ch1’,“ce1”,“lun1”,“TU2”],
[‘ch1’,“ce1”,“lun1”,“TU1”],
[‘ch1’,“ce1”,“lun1”,“TU1”],
[‘ch1’,“ce1”,“lun1”,“TU9”],
[‘ch1’,“ce1”,“lun1”,“TU1”],
[‘ch1’,“ce1”,“lun1”,“TU3”],
[‘ch1’,“ce1”,“lun1”,“TU8”],
[‘ch1’,“ce1”,“lun1”,“TU1”],
[‘ch1’,“ce1”,“lun1”,“TU5”],
[‘ch1’,“ce1”,“lun1”,“TU5”],
]
filer_obj={}
def probability(data):
for item in data:
address=item[-1] #获取存放的地址
tu_type=item[0]+item[1]+item[2]
# print(tu_type,666)
if address not in filer_obj.keys():#判断当前的tu是否存在于filter_obj的key中,如果没有给一个默认空list
filer_obj[address]=[]
# print(filer_obj[address],‘uuuu’)
if tu_type not in filer_obj[address]: #前三列元素是否在分类中的list中 本意是去重
filer_obj[address].append(tu_type)
# print(filer_obj)
#循环遍历生成的字典,并计算概率
for key in filer_obj:
num=len(filer_obj[key]) #当前tu出现的次数
sum=100 #总数
print("{}的概率是{}%".format(key,num/sum*100))
if name==“main”:
probability(data_list)