用Python编程写一个香农编码程序
时间: 2025-01-05 16:37:11 浏览: 3
香农编码是一种无损数据压缩算法,它基于信息熵的概念,用于估计每个字符出现的概率并据此分配最短的二进制码长度。下面是一个简单的Python版本的香农编码程序示例:
```python
import collections
def shannon_encoding(input_string):
# 统计输入字符串中每个字符及其频率
frequency = collections.Counter(input_string)
# 计算信息熵
def entropy(freq):
prob = freq / len(input_string)
return -sum(prob * math.log2(prob + 1e-9) for prob in prob.values())
total_entropy = entropy(frequency)
# 根据频率计算香农编码
encoded_dict = {}
for char, freq in frequency.items():
code_length = int(-freq * total_entropy / math.log2(freq))
encoded_dict[char] = f"{code_length:b}" # 二进制表示
return encoded_dict, total_entropy
# 示例用法
input_text = "Hello, World!"
encoded_info, total_entropy = shannon_encoding(input_text)
print("编码结果:", encoded_info)
print(f"总熵: {total_entropy:.4f} bits")
阅读全文