Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除bytes对象的前n个元素而不复制_Python_Python 3.x_Byte - Fatal编程技术网

Python 删除bytes对象的前n个元素而不复制

Python 删除bytes对象的前n个元素而不复制,python,python-3.x,byte,Python,Python 3.x,Byte,我想删除函数中字节参数的元素。我希望更改参数,而不是返回新对象 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:] 这里的解

我想删除函数中
字节
参数的元素。我希望更改参数,而不是返回新对象

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:]
这里的解决方案是什么?

只需使用:

它几乎类似于
字节
,但是可变的:


bytearray
类是一个范围为0的可变整数序列使用bytearray,如上面@mseivert所示,您可以使用


我想你只能用一种非传统的方式通过ctypes来实现这一点。字节是不可变的,它们不允许在创建新字节对象的情况下进行变异。这太糟糕了。。但既然我只在那个时候需要它,也许一个非传统的方式是可以的,为什么这个要求是存在的?如果你详细说明,也许会找到一个不同的解决方案。
>>> a = bytearray(b'abcdef')
>>> del a[1]
>>> a
bytearray(b'acdef')
>>> a = bytearray(b'abcdef')
>>> a[:3]
bytearray(b'abc')
>>> a = a[3:]
a
bytearray(b'def')