Since you have character mapping with a 3-bit string, bitarray does a good job of encoding and decoding lists of characters in and out of bit arrays:
from bitarray import bitarray
from random import choice
symbols = {
'0' : bitarray('000'),
'a' : bitarray('001'),
'b' : bitarray('010'),
'c' : bitarray('011'),
'd' : bitarray('100'),
'e' : bitarray('101'),
'f' : bitarray('110'),
'g' : bitarray('111'),
}
seedstring = ''.join(choice(symbols.keys()) for _ in range(40))
ba = bitarray()
ba.encode(symbols, seedstring)
print seedstring
print ba
ba_string = ba.tostring()
print repr(ba_string)
print len(ba_string)
Print
egb0dbebccde0gfdfbc0d0ccfcg0acgg0ccfga00
bitarray('10111101000010001010101001101110010100... etc.
'\xbd\x08\xaanQ\xf4\xc9\x88\x1b\xcf\x82\xff\r\xee@'
15
You can see that this list of 40 characters (120 bits) is encoded in 15-byte bitarray.
source
share