-1

I have a list of lists containing 32 individual bits. I want to separate these values into 4 strings of binary digits, each representing a byte.

My data looks like:

array = [[1, 0, 0, 0, 1, 0, 1, 0],
         [0, 1, 1, 0, 0, 0, 0, 0],
         [0, 0, 0, 0, 1, 0, 0, 0],
         [0, 0, 1, 1, 1, 0, 1, 1]]

The result should satisfy

array[0] == '10001010'
array[1] == '01100000'
array[2] == '00001000'
array[3] == '00111011'

Every solution I've tried leaves me with a string containing commas, such as '1,0,0,0,1,0,1,0'.

What is the simplest way to take each of these byte groupings and convert them into a string?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
ts_abbott
  • 3
  • 2
  • You should show the solution you tried. It sounds like you are joining with commas instead of an empty string. – Mark Tolonen Jul 08 '22 at 19:01
  • "but every solution I've tried leaves me with a string containing commas, such as 1,0,0,0,1,0,1,0" We can only possibly explain what is wrong with attempts that are actually shown to us. Please read [ask] and [mre]. "Thanks in advance." On every Stack Exchange site, [the best way to thank us is to ask as directly and clearly as possible, without such commentary](https://meta.stackexchange.com/questions/2950) - this is not a discussion forum. – Karl Knechtel Jul 08 '22 at 19:08
  • Anyway, it isn't clear specifically what the problem is here. It *sounds as if* you just need a way to convert one of the inner lists, `[1, 0, 0, 0, 1, 0, 1, 0]`, to the corresponding joined-up string ,`'10001010'`, and that you would be able to solve the problem if you had that. But in that case, *why mention* the list? If you *also* need help with applying code to each element of a list, that is a separate question: see e.g. https://stackoverflow.com/questions/25082410. – Karl Knechtel Jul 08 '22 at 19:27
  • But actually, dealing with the inner list is mostly the same question: you need to *convert each integer to a string* in order to join them, which is the same process of "applying code to each element". There is also a more specific version of that Q&A: https://stackoverflow.com/questions/3590165. On the other hand, if the *actual question* is "how do I join strings without putting commas in between, that is covered by https://stackoverflow.com/questions/12453580. Unless you had a *completely different approach* in mind? In that case, I can't guess what it was. – Karl Knechtel Jul 08 '22 at 19:30
  • @KarlKnechtel The way you explained it is what I had in mind, no different approach. My question was answered but I will look into those links you provided, thanks. – ts_abbott Jul 08 '22 at 19:40

2 Answers2

2

Use a list comprehension to iterate the items, and join each item by converting it to a string:

>>> array = [[1, 0, 0, 0, 1, 0, 1, 0],
...          [0, 1, 1, 0, 0, 0, 0, 0],
...          [0, 0, 0, 0, 1, 0, 0, 0],
...          [0, 0, 1, 1, 1, 0, 1, 1]]
>>> array = [''.join(str(n) for n in item) for item in array]
>>> array
['10001010', '01100000', '00001000', '00111011']
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
0

Try this:

arr1, arr2, arr3, arr4 = [''.join(map(str,elem)) for elem in arr]
print(arr1, arr2, arr3, arr4, sep='\n')

10001010
01100000
00001000
00111011

or without tuple unpacking:

arr = ["".join(map(str, elem)) for elem in arr]
print(arr)
['10001010', '01100000', '00001000', '00111011']

Then you can access them with arr[0] etc..

Rabinzel
  • 7,757
  • 3
  • 10
  • 30
  • Both of these methods worked exactly as I needed, thank you so much! In your opinion, would you consider the method without tuple unpacking cleaner code? – ts_abbott Jul 08 '22 at 19:06
  • I guess that depends on what you want to do with it. If you only have this 4 elements it is no problem to assign them to a variable, but if the data grows big you wouldn't do it. Or if the order of the strings matter for example you'd also keep them in the list propably. The unpacking was just to show you a more elegant way to handle the list instead of writing down array[0], array[1], array[2] for every element. – Rabinzel Jul 08 '22 at 19:12
  • 1
    `[''.join(map(str,elem)) for elem in arr]` is the interesting part of the code. What you do with the result after that depends on your requirements in the context of the rest of the program. – Karl Knechtel Jul 08 '22 at 19:12