【运行截图】
from math import *
class Shannon:
code_dict={}
code_length_list=[]
CumulativeProbabilityList=[]
x_p={}
x_plist=[]
AvgCodeLength=0
CodingEfficiency=0
def sort(self,data: str):
# 统计信源中各个概率
self.x_p = {} # 一个信源和概率对应的字典
x_list = []
for i in data:
if i not in x_list:
x_list.append(i)
length = len(data)
for i in x_list:
self.x_p[i] = data.count(i) / length
# 对概率进行降序排序 并返回一个列表
self.x_plist = sorted(self.x_p.items(), key=lambda kv: kv[1]) # 直接百度找来的字典跟据值进行排序
# 返回一个列表 列表元素是一个元组 元组有两个东西组成,分别是字符和概率
self.x_plist= self.x_plist[::-1]
return self.x_plist
def ShannonCoding(self): # 香农编码
self.code_length_list = [] # 存放各个码的码长
for i in self.x_plist: # 生成码长列表
self.code_length_list.append(int(1 - (log(i[1], 2))))
self.CumulativeProbabilityList = [] # 存放累加概率
# 第一位直接存0
self.CumulativeProbabilityList.append(0)
for i in range(1, len(self.x_plist)):
sum = 0
for j in range(0, i):
sum = self.x_plist[j][1]
self.CumulativeProbabilityList.append(sum)
self.code_dict = {} # 码字列表
for i in range(0, len(self.CumulativeProbabilityList)):
self.code_dict[self.x_plist[i][0]] = self.float2bin(self.CumulativeProbabilityList[i], self.code_length_list[i])#计算平均码长
avg = 0
for i in range(0, len(self.code_length_list)):
avg = self.code_length_list[i] * self.x_plist[i][1]
self.AvgCodeLength=avg
#计算编码效率
Hx = 0
for i in range(0,len(self.code_length_list)):
Hx =-(self.x_plist[i][1]*log(self.x_plist[i][1],2))
self.CodingEfficiency=Hx/self.AvgCodeLength
return self.code_dict
# 计算小数二进制
def float2bin(self,float, len=10):
res = ''
for i in range(0, len):
float = float * 2
if float >= 1:
res = '1'
float -= 1
else:
res = '0'
return res
评论