I ran into this performance issue when I tried to convert a huge 2D list of binary data to decimal numbers.
Given a list:
biglist = [
[[1,0],[0,0],[1,1]],
[[0,0],[1,1],[1,0]],
#...
#easily go to thousands of rows
]
In each row, I want combine all first element of each column and convert it into a decimal number:
E.g.
In row 0, I want int('101',2) i.e. 5
In row 1, I want int('011',2) i.e. 3
My final goal is to create a dictionary that counts what integer appears how many times. Considering the given data in the example above, the final result should be a dictionary with {key:value} pair like {a_int : appearance_count} like this:
{{5:1},{3:1}}
Now my solution is this:
result = {}
for k in biglist:
num = int("".join(str(row[0]) for row in k), 2)
#count
if num not in result:
result[num] = 1
else:
result[num] += 1
This loop is slow for a list of thousands of rows, is there a better solution?