I have a bytes list and want to get the last item, while preserving its bytes type. Using [-1] it gives out an int type, so this is not a direct solution.
Example code:
x = b'\x41\x42\x43' y = x[-1] print (y, type(y)) # outputs: 67 <class 'int'>
For an arbitrary index I know how to do it:
x = b'\x41\x42\x43' i = 2 # assume here a valid index in reference to list length y = x[i:i+1] print (y, type(y)) # outputs: b'C' <class 'bytes'>
Probably I can calculate the list length and then point an absolute length-1 number, rather than relative to list end.
However, is there a more elegant way to do this ? (i.e. similar to the simple [-1]) I also cannot imagine how to adapt the [i:i+1] principle in reverse from list end.
https://stackoverflow.com/questions/65911511/how-to-get-the-last-byte-item-from-a-bytes-list-in-python January 27, 2021 at 09:05AM
没有评论:
发表评论