作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个字典,我将其转换为字节数组,并且由于字节数组是不可变的(无法修改),我尝试创建一个等于字节数组中每个索引的列表。
a = {1:'a', 2:'b', 3:'c'}
b = bytearray(str(a), 'ASCII')
c = []
for i in b:
c[i] = b[i] # Error on this line
print(str(c))
问题是它一直打印IndexError: bytearray index out of range
。
bytearray 如何以及为什么超出范围?
最佳答案
如果我正确理解了你的问题,你可以简单地使用 c = list(b)
:
a = {1:'a', 2:'b', 3:'c'}
b = bytearray(str(a), 'ASCII')
c = list(b)
print(c)
输出:
[123, 49, 58, 32, 39, 97, 39, 44,
32, 50, 58, 32, 39, 98, 39, 44,
32, 51, 58, 32, 39, 99, 39, 125]
要了解为什么会出现此错误,请参阅 answer .
关于python - 如何将字节数组的内容复制到列表 (Python)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16843641/
我是一名优秀的程序员,十分优秀!