Context:
I'm coding up a simple genetic optimization algorithm from first principles, and have decided to use boolean/binary strings as genes so that I can flip bits on or off as a form of mutation. In order to evaluate the fitness of the solutions that it generates I need to be able to decode the binary strings produced into the integer values that I'm optimizing for. The genetic optimization technique that I'm using returns it's individuals as a list, so I am extracting the integers as follows:
Problem:
Take the boolean/binary list...
l = [1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1]
...split it into 8-bit sublists...
[[1, 0, 1, 0, 0, 1, 1, 1], [0, 0, 1, 0, 1, 0, 1, 0], [0, 0, 1, 1, 1, 0, 1, 1]]
...concatenate each sublist into a string...
[10100111, 101010, 111011]
...convert those strings from binary to decimal...
[167, 42, 59]
...then scale those integers between 50 and 150...
[115, 66, 73]
My solution:
[int(int(str(bin), 2)/2.55)+50 for bin in [int(''.join(\
map(str,num))) for num in [l[i:i +8] for i in range(0, len(l), 8)]]]
This feels like a very ugly way to go tackle the problem - can anyone come up with a nicer way to go about it?