简体   繁体   English

使用Python将整数和字符串列表压缩到文件中

[英]Compressing a list of integers and strings into file with Python

I have a list that is made of strings made up from "A" and "B", and integers withing range 0..255. 我有一个列表,该列表由“ A”和“ B”以及范围为0..255的整数组成的字符串组成。 For instance array can look like this: 例如,数组可以如下所示:

L = ["ABA", 2, 3, 100, 1, "BB"]

I want to put it in file as a purpose of compression (list L is the end product of BWT+MTF+Run-length transformations). 我想将其放在文件中以进行压缩(列表L是BWT + MTF + Run-length转换的最终产品)。 I also want to read it from that file. 我也想从该文件中读取它。 This is what i have tried with bytearray, but since "BB" is not one byte, it fails: 这是我尝试使用bytearray的方法,但是由于“ BB”不是一个字节,因此它失败了:

def compresslist(L, filename):

f = open(filename, 'wb')
f.write(bytearray(L))
f.close()

strings made up from "A" and "B" 由“ A”和“ B”组成的字符串

L = ["ABA", 2, 3, 100, 1, "BB"]

A simple compression for "ABAA" would be to use 1 bit per character: 0 for 'A' and 1 for 'B' "ABAA"简单压缩将是每个字符使用1位: 'A'为0, 'B'为1

If the strings never become longer than 8, they can be stored in 1 byte . 如果字符串长度不超过8,则可以将其存储在1 byte This would only work if the structure of your list is fixed, ie you know where the bytes and the integers are. 这仅在列表结构固定的情况下才有效,即您知道bytesintegers在哪里。

If the integers were all smaller than 128, strings (max 7 chars) could be stored in 7 bits, with the most significant bit to indicate it's a compressed string. 如果所有integers均小于128,则strings (最多7个字符)可以7位存储,其中最高有效位指示其为压缩字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM