如果您对如何在Python中打包任意位序列?感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于如何在Python中打包任意位序列?的详细内容,我们还将为您解答如何在python
如果您对如何在Python中打包任意位序列?感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于如何在Python中打包任意位序列?的详细内容,我们还将为您解答如何在python中打包任意位序列数据的相关问题,并且为您提供关于在python中打包二进制浮点数、在python中打包字符串的正确方法、如何在python中“按任意键”?、如何在Python中以相反的顺序迭代一个序列?的有价值信息。
本文目录一览:- 如何在Python中打包任意位序列?(如何在python中打包任意位序列数据)
- 在python中打包二进制浮点数
- 在python中打包字符串的正确方法
- 如何在python中“按任意键”?
- 如何在Python中以相反的顺序迭代一个序列?
如何在Python中打包任意位序列?(如何在python中打包任意位序列数据)
我想将一些二进制图像数据编码/压缩为序列(如果有位)。(通常,此序列的长度不能完全适合所有标准整数类型。)
我如何做到这一点而又不浪费空间?(我意识到,除非位序列的长度为“ nice”,否则在最后一刻始终必须有少量[<1个字节]的剩余空间。)
FWIW,我估计每个要编码的符号最多需要3位。Python是否有用于此类工作的内置工具?
答案1
小编典典内置的东西并不十分方便,但是为此目的设计了第三方模块,例如bitstring和bitarray。
from bitstring import BitArrays = BitArray(''0b11011'')s += ''0b100''s += ''uint:5=9''s += [0, 1, 1, 0, 1]...s.tobytes()
要将3位数字序列(即范围0-> 7)结合在一起,可以使用
>>> symbols = [0, 4, 5, 3, 1, 1, 7, 6, 5, 2, 6, 2]>>> BitArray().join(BitArray(uint=x, length=3) for x in symbols)BitArray(''0x12b27eab2'')>>> _.tobytes()''\x12\xb2~\xab ''
在python中打包二进制浮点数
在执行二进制文件写入时,我在打包和解包python中的二进制浮点数时遇到了一些麻烦。这是我所做的:
import struct
f = open('file.bin','wb')
value = 1.23456
data = struct.pack('f',value)
f.write(data)
f.close()
f = open('file.bin','rb')
print struct.unpack('f',f.read(4))
f.close()
我得到的结果如下:
(1.2345600128173828,)
多余的数字怎么回事?这是舍入错误吗?这是如何运作的?
在python中打包字符串的正确方法
字节序仅是值大于一个字节的东西,因此您不必为此担心。
我想到的最简洁的方法是将5字节字节字符串填充为8字节
b'David'.ljust(8,b'\0')
如何在python中“按任意键”?
我将如何在Python中“按任意键”(或抓住菜单选项)?
- raw_input要求您按回车键。
- Windows msvcrt具有getch()和getche()。
有使用标准库执行此操作的可移植方法吗?
如何在Python中以相反的顺序迭代一个序列?
Python 序列包括字符串、列表、元组等。我们可以使用不同的方式合并 Python 序列的元素。让我们看一些以相反顺序迭代列表的示例。
使用while循环以逆序迭代
示例
在这个例子中,我们有一个作为序列的列表,并使用while循环以相反的顺序进行迭代 -
# Creating a List mylist = ["Jacob", "Harry", "Mark", "Anthony"] # Displaying the List print("List = ",mylist) # Length - 1 i = len(mylist) - 1 # Iterate in reverse order print("Display the List in Reverse order = ") while i >= 0 : print(mylist[i]) i -= 1
输出
List = [''Jacob'', ''Harry'', ''Mark'', ''Anthony''] Display the List in Reverse order = Anthony Mark Harry Jacob
使用for循环以逆序迭代
示例
在这个例子中,我们有一个作为序列的列表,并使用for循环以相反的顺序进行迭代-
# Creating a List mylist = ["Jacob", "Harry", "Mark", "Anthony"] # Displaying the List print("List = ",mylist) # Iterate in reverse order print("Display the List in Reverse order = ") for i in range(len(mylist) - 1, -1, -1): print(mylist[i])
输出
List = [''Jacob'', ''Harry'', ''Mark'', ''Anthony''] Display the List in Reverse order = Anthony Mark Harry Jacob
使用reverse()逆序迭代
示例
在这个例子中,我们有一个List作为一个序列,并使用reversed()方法以相反的顺序进行迭代 -
立即学习“Python免费学习笔记(深入)”;
# Creating a List mylist = ["Jacob", "Harry", "Mark", "Anthony"] # Displaying the List print("List = ",mylist) # Iterate in reverse order using reversed() print("Display the List in Reverse order = ") [print (i) for i in reversed(mylist)]
输出
List = [''Jacob'', ''Harry'', ''Mark'', ''Anthony''] Display the List in Reverse order = Anthony Mark Harry Jacob
使用负索引进行反向迭代
示例
在此示例中,我们将 List 作为序列,并使用负索引以相反顺序迭代它 −
# Creating a List mylist = ["Jacob", "Harry", "Mark", "Anthony"] # Displaying the List print("List = ",mylist) # Iterate in reverse order using negative indexing print("Display the List in Reverse order = ") [print (i) for i in mylist[::-1]]
输出
List = [''Jacob'', ''Harry'', ''Mark'', ''Anthony''] Display the List in Reverse order = Anthony Mark Harry Jacob
以上就是如何在Python中以相反的顺序迭代一个序列?的详细内容,更多请关注php中文网其它相关文章!
关于如何在Python中打包任意位序列?和如何在python中打包任意位序列数据的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于在python中打包二进制浮点数、在python中打包字符串的正确方法、如何在python中“按任意键”?、如何在Python中以相反的顺序迭代一个序列?的相关信息,请在本站寻找。
本文标签: