我是初学者,我相信这个问题太简单了。我正在尝试在 cython 中测试内存视图以更好地了解它们。在我的代码中,我将每个内存视图元素(如[1,2]
)作为cy
类元素传递move
。
cdef class cy:
cdef public long[:] move
def __init__(self, move):
self.move = move
lst = []
for i in range(100):
lst.append([i, i+1])
cdef long[:, :] memview = np.asarray(lst)
b0 = cy(memview[0])
print(b0.move)
当我打印结果时。我明白了:
<MemoryView of 'ndarray' object> # I expect for sth like [12, 13]
我需要cy
班级打印一份清单。我该如何解决?当我使用此代码时,我遇到了另一个问题:
cdef class parent:
cdef public:
list children
list moves
def __init__(self):
self.children = []
def add_children(self, moves):
cdef int i = 0
cdef int N = len(moves)
for i in range(N):
self.children.append(cy(moves[i]))
cdef int[:, :] moves = np.asarray(lst, dtype=np.int32)
obj = parent()
for move in moves:
obj.add_children(move)
运行此代码后,我总是收到此错误:
TypeError: a bytes-like object is required, not 'int'
. 是什么导致了这个错误,我该如何解决这个错误?