y-s*_*een 0 python byte python-3.x
我想删除bytes
函数中参数的元素。我希望更改参数,而不是返回新对象。
def f(b: bytes):
b.pop(0) # does not work on bytes
del b[0] # deleting not supported by _bytes_
b = b[1:] # creates a copy of b and saves it as a local variable
io.BytesIO(b).read(1) # same as b[1:]
Run Code Online (Sandbox Code Playgroud)
这里的解决方案是什么?
只需使用一个bytearray
:
>>> a = bytearray(b'abcdef')
>>> del a[1]
>>> a
bytearray(b'acdef')
Run Code Online (Sandbox Code Playgroud)
它几乎就像bytes
但可变的:
的
bytearray
类是整数的范围为0一个可变的序列<= X <256它已大部分的可变序列的常规方法,在所描述的可变序列类型,以及大多数方法,该bytes
类型有,见字节和ByteArray操作.
归档时间: |
|
查看次数: |
11999 次 |
最近记录: |