Python - 相同的数组,不同的字节表示
Python - Same array, different bytes representation
谁能帮我理解为什么这两个数组不一样:
加载数组,执行:
import base64
import array
a1 = array.array('I', base64.b64decode(data_base64))
此方法断言两个数组相等:
def assert_equal_arrays(a1, a2) -> None:
if len(a1) != len(a2):
raise RuntimeError(f'Lengths are not same a1:{len(a1)}, a2:{len(a2)}')
for i in range(0, len(a1) - 1):
if (a1[i] != a2[i]):
raise RuntimeError(f'Elements at {i} are not the equal, a1[{i}]:{a1[i]}, a2[{i}]:{a2[i]}')
if (type(a1[i]) != type(a2[i])):
raise RuntimeError(f'Elements at {i} are not the same type, type(a1[{i}]):{type(a1[i])}, a2[{i}]:{type(a2[i])}')
当我传递 array
对象时它不会失败,但是当我传递 bytes
:
时它会失败
assert_equal_arrays(a1, a2) #Pass
assert_equal_arrays(a1.tobytes(), a2.tobytes()) #Fails
我错过了什么?
提前致谢。
这是因为你的循环中有 range(0, len(a1) - 1)
。
应该是range(0, len(a1))
另外..我只是运行这个
with open("../../../Downloads/result_db_array.txt") as f:
a = f.read()
with open("../../../Downloads/full_db_array.txt") as ff:
b = ff.read()
for i,x in enumerate(a):
if b[i] != x:
print(f"({i},{x},{b[i]})")
这是输出。
(11661818,S,T)
如果它们的字符串不相等,则 base64 数组也不相等。
谁能帮我理解为什么这两个数组不一样:
加载数组,执行:
import base64
import array
a1 = array.array('I', base64.b64decode(data_base64))
此方法断言两个数组相等:
def assert_equal_arrays(a1, a2) -> None:
if len(a1) != len(a2):
raise RuntimeError(f'Lengths are not same a1:{len(a1)}, a2:{len(a2)}')
for i in range(0, len(a1) - 1):
if (a1[i] != a2[i]):
raise RuntimeError(f'Elements at {i} are not the equal, a1[{i}]:{a1[i]}, a2[{i}]:{a2[i]}')
if (type(a1[i]) != type(a2[i])):
raise RuntimeError(f'Elements at {i} are not the same type, type(a1[{i}]):{type(a1[i])}, a2[{i}]:{type(a2[i])}')
当我传递 array
对象时它不会失败,但是当我传递 bytes
:
assert_equal_arrays(a1, a2) #Pass
assert_equal_arrays(a1.tobytes(), a2.tobytes()) #Fails
我错过了什么?
提前致谢。
这是因为你的循环中有 range(0, len(a1) - 1)
。
应该是range(0, len(a1))
另外..我只是运行这个
with open("../../../Downloads/result_db_array.txt") as f:
a = f.read()
with open("../../../Downloads/full_db_array.txt") as ff:
b = ff.read()
for i,x in enumerate(a):
if b[i] != x:
print(f"({i},{x},{b[i]})")
这是输出。
(11661818,S,T)
如果它们的字符串不相等,则 base64 数组也不相等。