Python实现的哈夫曼编码
发布时间:2020-05-24 23:33:44 所属栏目:Python 来源:互联网
导读:Python实现的哈夫曼编码
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 #coding:utf-8
import struct
codeDict={}#全局字典key=字符,value=数字
encodeDict={}
filename=None
listForEveryByte=[]
class Node:
def __init__(self,right=None,left=None,parent=None,weight=0,charcode=None):
self.right=right
self.left=left
self.parent=parent
self.weight=weight
self.charcode=charcode
#按权值排序
def sort(list):
return sorted(list,key=lambda node:node.weight)
#构建哈夫曼树
def Huffman(listOfNode):
listOfNode=sort(listOfNode)
while len(listOfNode)!=1:
a,b = listOfNode[0],listOfNode[1]
new=Node()
new.weight,new.left,new.right = a.weight + b.weight,a,b
a.parent,b.parent = new,new
listOfNode.remove(a),listOfNode.remove(b)
listOfNode.append(new)
listOfNode=sort(listOfNode)
return listOfNode
def inPutFile():
global filename
global listForEveryByte
filename=raw_input("请输入要压缩的文件:")
global codeDict
with open(filename,'rb') as f:
data=f.read()
for Byte in data:
codeDict.setdefault(Byte,0) #每个字节出现的次数默认为0
codeDict[Byte]+=1
listForEveryByte.append(Byte)
def outputCompressedFile():
global listForEveryByte
fileString=""
with open(filename.split(".")[0]+".jbj","wb") as f:
for Byte in listForEveryByte:
fileString+=encodeDict[Byte] #构成一个长字符序列
leng=len(fileString)
more=16-leng%16
fileString=fileString+"0"*more #空位用0补齐
#print(fileString)
leng=len(fileString)
i,j=0,16
while j<=leng:
k=fileString[i:j]
a=int(k,2)
#print(a)
# print(repr(struct.pack(">H",a)))
f.write(struct.pack(">H",a))
# f.write(str(a))
i=i+16
j=j+16
def encode(head,listOfNode):
global encodeDict
for e in listOfNode:
ep=e
encodeDict.setdefault(e.charcode,"")
while ep!=head:
if ep.parent.left==ep:
encodeDict[e.charcode]="1"+encodeDict[e.charcode]
else:
encodeDict[e.charcode]="0"+encodeDict[e.charcode]
ep=ep.parent
if __name__ == '__main__':
inPutFile()
listOfNode=[]
for e in codeDict.keys():
listOfNode.append(Node(weight=codeDict[e],charcode=e))
head=Huffman(listOfNode)[0] #构建哈夫曼树,head称为树的根节点
encode(head,listOfNode)
for i in encodeDict.keys():
print(i,encodeDict[i])
#outputCompressedFile()
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
