I have string of bytes that come from embeded system and I use python struct.unpack() to give me tuple of values.
As there are a lot of values I wish to make code more readable. For now I have it done like this:
ArrA = [None] * 3
ArrB = [None] * 2
ArrA[0],ArrA[1],ArrA[2], n, ArrB[0],ArrB[1], crc = (1, 2, 3, 4, 5, 6, 7)
# tuple came from unpack()
Result:
ArrA = [1, 2, 3]
n = 4
ArrB = [5, 6]
crc = 7
Is there some way to shorten arrays? eg ArrA[0],ArrA[1],ArrA[2] to something like ArrA[]*3??
In example arrays are short in real they are pretty long.
I don't think there's a way to do this with unpacking. If you had only one list, you could use a starred expression*, but not with multiple lists.
Here are some alternatives:
Basic indexing and slicing
One difference is that you won't get an error if
tis too long, but you can simply add a check, e.g:Iterator
If you'd rather focus on the number of elements in the lists rather than their positions in
t, you can maketinto an iterator and consume it a chunk at a time:This lets you check that
tis not too long using this weird syntax:Instead of using list comprehensions,
isliceis cleaner:Pop from a stack (list)
Python lists are like stacks, with pops happening from the end by default. So we just need to convert
tto a list and reverse it to be able to use it similarly to the iterator solution above. It's also possible to pop from the start, but it's less efficient at large scales and I think this is cleaner anyway.Footnotes
* For example:
Result: