0

I have every long binary string that I would like to filter by a pattern. Here is a working example:

x = b"\x00\x01\x02\x03\x04\x00\x01\x02\x03\x04"
x[1]
y = [x[i] for i in range(len(x)) if not ((i%5 == 4) or (i%5 == 3))]
bytes(y)

It works fine, but I am wondering if there is a better method, performance-wise. I use python 3.4, if that matters.

1 Answer 1

1

You should probably create a generator instead of a list:

bytes(x[i] for i in range(len(x)) if (i % 5) not in [3, 4])

and not:

bytes([x[i] for i in range(len(x)) if (i % 5) not in [3, 4]]) # DON'T DO THIS

This will save you memory (and experience show it will also be a bit faster). I cannot think of another (more efficient) method.

Sign up to request clarification or add additional context in comments.

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.