Home > Mobile >  how can I split the elements of a list of binary numbers?
how can I split the elements of a list of binary numbers?

Time:09-26

I'm trying to split the elements of a list of binary numbers as follows :

bin_list=['000101111000','011110111011']

Expected result: bin_list=[[0,0,0,1,0,1,1,1,1,0,0,0],[0,1,1,1,1,0,1,1,1,0,1,1]]

I think that would be a list of a list of integers what I'm trying to get?? I tried searching a similar procedure and I've tried some but I can't get it right pls help

thanks!!

CodePudding user response:

This could be done with nesting list comprehensions:

>>> bin_list = ['000101111000','011110111011']
>>> [[int(n) for n in ns] for ns in bin_list]
[[0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0], [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1]]

CodePudding user response:

It should be as simple as follows:

result = [list(map(int, k)) for k in bin_list]

list(map(int, k)) should basically split the string into a list with individual characters cast as integers, as elements.

  • Related