Python Codes

Handling Array of Long Bit Strings – Python

When working with data science projects or any other as well, we might need to work  with long binary strings. Bitarray is the package that is need of the hour. However we might also need an array of bit strings. In that case, one option is to store every bit string in a dictionary data structure.  Every bit string might have an associated value to it. That value can go inside the dictionary as a key. If no such value exists then natural numbers can be used as key.

Below is an example code to work with set of binary strings. The code is sufficiently commented to set the necessary understanding. The code is written in python 3.4.

# import the bitarray
from bitarray import bitarray

# Global list data structure to hold long string
# For sample example we have considered strings containing . and *
string_list = ['..*....********.....*****', '..........**********..***', '***.......******....****.', '**********........*****..']

def prepare_bit_strings():
    # A dictionary to hold the binary strings
    bit_string_dict = {}

    # ID number which will act as key
    id_num = 1

    # Use the string list initialized
    global string_list

    # Iterate through the given list
    # and convert them to bit strings
    # Every * is assigned to 0
    # Every . is assigned to 1
    for kcount in range(0, len(string_list)):
        a = bitarray()
        line = string_list[kcount]
        for count in range (0, len(line)):
            if line[count] == "*":

        bit_string_dict[id_num] = a
        id_num  = id_num + 1        

    # Print the generated dictionary



Let me Know What you Think!

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s