【知识发现】python开源哈夫曼编码库huffman
生活随笔
收集整理的這篇文章主要介紹了
【知识发现】python开源哈夫曼编码库huffman
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1、哈夫曼樹:
? ?安裝:pip install huffman
? ?pypi地址:https://pypi.python.org/pypi/huffman
? ?源碼很值得參考。
2、案例:
# -*- coding: utf-8 -*- ''' Created on 2017年9月26日@author: Administrator ''' import huffman import collectionst1=huffman.codebook([('A', 2), ('B', 4), ('C', 1), ('D', 1)]) print (t1) t2=huffman.codebook(collections.Counter('man the stand banana man').items()) print (t2)說明:rovided an iterable of 2-tuples in (symbol, weight) format, generate a Huffman codebook, returned as a dictionary in {symbol: code, ...} format.
3、構(gòu)造哈夫曼樹參考:
#構(gòu)造哈夫曼樹 import heapq trees=huff_df.values.T.tolist() #dataframe轉(zhuǎn)化成list heapq.heapify(trees) while len(trees)>1:rightChild,leftChild=heapq.heappop(trees),heapq.heappop(trees)parentNode=(leftChild[0]+rightChild[0],leftChild,rightChild)heapq.heappush(trees,parentNode) print (trees)huff_df是一個(gè)dataframe,轉(zhuǎn)化成list,里面是結(jié)點(diǎn)及其頻率。
總結(jié)
以上是生活随笔為你收集整理的【知识发现】python开源哈夫曼编码库huffman的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【数据平台】sqoop支持hive和my
- 下一篇: 【数据平台】同一mysql主机不同数据库