Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
transform list of integers into bitmaps bytes and back
import numpy as np
ids = [1, 5, 7, 12]
bitmaps = np.zeros(max(ids) + 1, dtype=np.bool)
bitmaps[ids] = True
bitmaps = np.packbits(bitmaps).tostring()
print(bitmaps)
# b'E\x08'
# could be use as a binary field to be stored in database
origin_ids = np.where(
np.unpackbits(
np.frombuffer(bitmaps,
dtype=np.uint8)))[0].tolist()
print(origin_ids)
# [1, 5, 7, 12]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.