Reputation: 47
I have a list of binary number like this:
['11100', '11010', '11001', '10110', '10101', '10011', '01110', '01101', '01011', '00111']
Is there a method to create a nested list containing sublists in which the elements are the single digit of the bynary number?
new_list = [[1, 1, 1, 0, 0], [1, 1, 0, 1, 0], [1, 1, 0, 0, 1], [1, 0, 1, 1, 0], [1, 0, 1, 0, 1], [1, 0, 0, 1, 1], [0, 1, 1, 1, 0], [0, 1, 1, 0, 1], [0, 1, 0, 1, 1], [0, 0, 1, 1, 1]]
Thanks in advance!
Upvotes: 2
Views: 97
Reputation: 4795
Another possible solution is this one:
oldlist = ['11100', '11010', '11001', '10110', '10101', '10011', '01110', '01101', '01011', '00111']
newlist = [list(map(int,list(x))) for x in oldlist]
print(newlist)
And gives this output:
[[1, 1, 1, 0, 0], [1, 1, 0, 1, 0], [1, 1, 0, 0, 1], [1, 0, 1, 1, 0], [1, 0, 1, 0, 1], [1, 0, 0, 1, 1], [0, 1, 1, 1, 0], [0, 1, 1, 0, 1], [0, 1, 0, 1, 1], [0, 0, 1, 1, 1]]
Upvotes: 1
Reputation: 4771
Use a list comprehension like this
>>> l = ['11100', '11010', '11001', '10110',
'10101', '10011', '01110', '01101',
'01011', '00111']
>>> new_list = [[int(i) for i in j] for j in l]
>>> print(new_list)
[[1, 1, 1, 0, 0],
[1, 1, 0, 1, 0],
[1, 1, 0, 0, 1],
[1, 0, 1, 1, 0],
[1, 0, 1, 0, 1],
[1, 0, 0, 1, 1],
[0, 1, 1, 1, 0],
[0, 1, 1, 0, 1],
[0, 1, 0, 1, 1],
[0, 0, 1, 1, 1]]
Upvotes: 4