What is the best way to merge 2 byte arrays?

Bob5421

Let's suppose i have 2 bigs arrays (i have put smaller arrays in this example):

a1=bytes([10,20,30,40,50,60,70,80])
a2=bytes([11,21,31,41,51,61,71,81])

What i want to do is to merge this 2 arrays this way:

[10,20 , 11,21 , 30,40 , 31,41 , ...

What i want is to take 2 bytes from first array, then 2 from second array, etc.

Here is what i done. It works but i think there is a best way to do this, without having to create intermediates arrays:

a3 = bytearray()
for i in range(0, len(a1), 2):
    a3.append(a1[i])
    a3.append(a1[i+1])
    a3.append(b1[i])
    a3.append(b1[i+1])

output_array=bytes(a3)  # Very important: i need bytes() object at the end
Dekel

Chunk taken from here: How do you split a list into evenly sized chunks?
Merge taken from here: How do I merge two lists into a single list?

Combined into this:

def chunks(l, n):
    """Yield successive n-sized chunks from l."""
    for i in range(0, len(l), n):
        yield l[i:i + n]

a1 = [10,20,30,40,50,60,70,80]
b1 = [11,21,31,41,51,61,71,81]
combined = [j for i in zip(chunks(a1, 2),chunks(b1, 2)) for j in i]
out = bytes(bytearray([x for pair in combined for x in pair]))
==> b'\n\x14\x0b\x15\x1e(\x1f)2<3=FPGQ'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the best way to "merge" 2 arrays by each value?

What is the best way to merge nested arrays

What is the best way to merge hashes?

What is the best way to merge two arrays (element + element), if elements itself are arrays

What's the best way to merge to arrays based on key values in each array?

What is the best way to convert a byte array to an IntStream?

What is the best way to store a byte array in memory

linux + what the best way to merge files

What is the best way to merge development database with production?

What is the best way to merge multiple Flink DataStreams?

What is the best way to reduce and merge a collection of objects

What is the time complexity of this program that slices array into 2 arrays and sums them and what would be the best way to reduce it?

Best way to combine two or more byte arrays in C#

Fast way to compare 2 byte arrays

What is the best way to Serialize/Deserialize nested Tuple2 arrays in dart (List<List<Tuple2>>)

What's faster? Merge byte arrays or send separately?

What's the best way to merge 2 tables keeping 1 column aligned?

Best way to merge two Arrays of Classes based on Class variable value

Best way to merge and transform keys and arrays from an object

Best way Merge two string arrays based on a condition

What's the best way to document an array of arrays of arrays using JSDoc?

What’s the best way to iterate 2 arrays correspondingly when using numpy?

What is the best way to compare 2 arrays and echo out the values of the same content of the array in php?

What is the best way to create byte data structure not fixed size in JAVA

What is the best way to merge a feature into an older release branch?

What is the best (and safest) way to merge a Git branch into master?

What is best way to merge detached entities to entity manager in JPA

What is the best way to resolve merge conflicts in .swm files?

What's the best way to merge to the same table several times in Postgresql?