0

what would be the fastest way to put the first 4 bytes of an 8 byte array in one array and the last 4 bytes in another one.

My approach was to create a for loop and then extract everything. Like this

for i in range(0,7):
    if i < 4:
        ...
    else
        ...

There has to be something more efficient. What am I missing?

2
  • what object do you call byte array? and what do you optimise, number of lines, excetuion time, something else? Commented Dec 9, 2013 at 22:13
  • Your for loop doesn't do anything with the last byte. You should type in range(0,7) at a Python 2 command line (or list(range(0,7)) at a Python 3 command line) and see what you get. Commented Dec 9, 2013 at 22:21

4 Answers 4

1

Try

hi, lo = some_array[:4], some_array[4:]
Sign up to request clarification or add additional context in comments.

Comments

1
a = range(0,8)
b = a[:4]
c = a[4:]

Easiest way I know.

Comments

0

Use slices:

A = [1,2,3,4,5,6,7,8]
A[0:4]
A[4:8]

Comments

0

List slicing is your friend here ;)

array = [0,1,2,3,4,5,6,7]

firstpart,secondpart = (array[:4],array[4:])

print firstpart
[0, 1, 2, 3]
print secondpart
[4, 5, 6, 7]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.