Home > Mobile >  How to split binary array into two?
How to split binary array into two?

Time:12-15

Here is my array:

['1011000111001100', '0010110101100111', '0010101011111111', '1010111001101001', '1010110101000101', '1001011111001001', '1010110000001111', '1011000000111001', '0011000101001101', '1011000110100100', '0011000010100110', '1010100111110100', '1011000010100111', '1010000100111000', '1010110001110011', '0011001100101101', '1010111101011010', '1011000011010010', '1010111100010100', '1010101111001000', '0001100000111110', '1010100000010111', '1001110111101000', '0010110111000000', '1010111001011001', '1011011010010011', '1010111100010110', '1010110001010100', '1010011111100000', '1010100001111100', '1010101100110100', '0010101000001000', '1011001010010111', '1010100000000001', '0010010010000011', '0010111001100101', '0001100101010100', '1010100111001011', '0011000001110110', '1010001010101101', '1011001001100000', '0001100001110000', '0011000101101111', '0011001111111010', '0011000101100000', '0010100001011110', '0010110000001000', '1010101010010100', '0010111111001110', '1011010000001100', '1000111110000101', '0010100101101001', '0010110100111011', '0010011101110110', '0010111111101001', '1011000010010111', '0010101001011010', '0011000000000011', '1011000101001100', '0010110010001011', '0010000000001101', '1010111001000101', '1010110011100010', '1010100101000111']

Now I need to split each element into 2. What I mean is from '1011000111001100' to '10110001', '11001100'. How will I do that? Please help me to make program about this

CodePudding user response:

You would iterate (loop) through each element of the list, split the current element and add it to another list (or whatever fits your problem better)

You can split a string using the string slicing operator:

s = "Hello, World!"
print(s[:3])
# "Hel"
splittedBinaryArray = []

for element in binaryArray:
    leftSide = element[:8] # From start to 16th bit
    rightSide = element[8:] # From 16th bit to end
    
    splittedBinaryArray.append([leftSide, rightSide])  # Add to new array

This is the result:

[['10110001', '11001100'], ['00101101', '01100111'], ['00101010', '11111111'], ...]

You maybe want to even convert the binary to an integer value if needed:

int("10110001", 2)  # Base 2
# 89

CodePudding user response:

Code:-

lis=['1011000111001100', '0010110101100111', '0010101011111111', '1010111001101001', '1010110101000101', '1001011111001001', '1010110000001111', '1011000000111001', '0011000101001101', '1011000110100100', '0011000010100110', '1010100111110100', '1011000010100111', '1010000100111000', '1010110001110011', '0011001100101101', '1010111101011010', '1011000011010010', '1010111100010100', '1010101111001000', '0001100000111110', '1010100000010111', '1001110111101000', '0010110111000000', '1010111001011001', '1011011010010011', '1010111100010110', '1010110001010100', '1010011111100000', '1010100001111100', '1010101100110100', '0010101000001000', '1011001010010111', '1010100000000001', '0010010010000011', '0010111001100101', '0001100101010100', '1010100111001011', '0011000001110110', '1010001010101101', '1011001001100000', '0001100001110000', '0011000101101111', '0011001111111010', '0011000101100000', '0010100001011110', '0010110000001000', '1010101010010100', '0010111111001110', '1011010000001100', '1000111110000101', '0010100101101001', '0010110100111011', '0010011101110110', '0010111111101001', '1011000010010111', '0010101001011010', '0011000000000011', '1011000101001100', '0010110010001011', '0010000000001101', '1010111001000101', '1010110011100010', '1010100101000111']
res=[]
for binary in lis:
    res.append(binary[:len(binary)//2])
    res.append(binary[len(binary)//2:])
print(res)

Output:-

['10110001', '11001100', '00101101', '01100111', '00101010', '11111111', '10101110', '01101001', '10101101', '01000101', '10010111', '11001001', '10101100', '00001111', '10110000', '00111001', '00110001', '01001101', '10110001', '10100100', '00110000', '10100110', '10101001', '11110100', '10110000', '10100111', '10100001', '00111000', '10101100', '01110011', '00110011', '00101101', '10101111', '01011010', '10110000', '11010010', '10101111', '00010100', '10101011', '11001000', '00011000', '00111110', '10101000', '00010111', '10011101', '11101000', '00101101', '11000000', '10101110', '01011001', '10110110', '10010011', '10101111', '00010110', '10101100', '01010100', '10100111', '11100000', '10101000', '01111100', '10101011', '00110100', '00101010', '00001000', '10110010', '10010111', '10101000', '00000001', '00100100', '10000011', '00101110', '01100101', '00011001', '01010100', '10101001', '11001011', '00110000', '01110110', '10100010', '10101101', '10110010', '01100000', '00011000', '01110000', '00110001', '01101111', '00110011', '11111010', '00110001', '01100000', '00101000', '01011110', '00101100', '00001000', '10101010', '10010100', '00101111', '11001110', '10110100', '00001100', '10001111', '10000101', '00101001', '01101001', '00101101', '00111011', '00100111', '01110110', '00101111', '11101001', '10110000', '10010111', '00101010', '01011010', '00110000', '00000011', '10110001', '01001100', '00101100', '10001011', '00100000', '00001101', '10101110', '01000101', '10101100', '11100010', '10101001', '01000111']

CodePudding user response:

considering you want to devide it in half

x = ['1011000111001100', '0010110101100111', '0010101011111111', '1010111001101001', '1010110101000101', '1001011111001001', '1010110000001111', '1011000000111001', '0011000101001101', '1011000110100100', '0011000010100110', '1010100111110100', '1011000010100111', '1010000100111000', '1010110001110011', '0011001100101101', '1010111101011010', '1011000011010010', '1010111100010100', '1010101111001000', '0001100000111110', '1010100000010111', '1001110111101000', '0010110111000000', '1010111001011001', '1011011010010011', '1010111100010110', '1010110001010100', '1010011111100000', '1010100001111100', '1010101100110100', '0010101000001000', '1011001010010111', '1010100000000001', '0010010010000011', '0010111001100101', '0001100101010100', '1010100111001011', '0011000001110110', '1010001010101101', '1011001001100000', '0001100001110000', '0011000101101111', '0011001111111010', '0011000101100000', '0010100001011110', '0010110000001000', '1010101010010100', '0010111111001110', '1011010000001100', '1000111110000101', '0010100101101001', '0010110100111011', '0010011101110110', '0010111111101001', '1011000010010111', '0010101001011010', '0011000000000011', '1011000101001100', '0010110010001011', '0010000000001101', '1010111001000101', '1010110011100010', '1010100101000111']
result = [i[j:j len(i)//2] for i in x for j in range(0, len(i), len(i)//2)]  

CodePudding user response:

(Arrays in Python are called "lists" so don't be confused if I call them that in this answer.)

This question is actually 2 questions:

  1. how to split string into two parts?

Assuming the string always have 16 characters, you can use the slicing operator:

left_part = your_string[:8]
right_part = your_string[8:]

Or if the length of the string can vary:

left_part = your_string[:len(your_string)//2]
right_part = your_string[len(your_string)//2:]
  1. How to convert a list to another one that have a different number of elements?

You can use a list comprehension:

new_array = [y for x in old_array for y in split_in_two(x)]

This iterates over the original list, splitting each of its element into 2, then it iterates ever these halves and create a new list out of them. If you don't like python list comprehensions, you can write the same in a more verbose way:

new_list = []
for x in old_list:
    for y in split_in_two(x):
        new_array.append(y)

If you put it all together, you get:

new_list = [y for x in old_list for y in (x[:8], x[8:])]
  • Related