0
我是一名初学者,我相信这个问题太简单了。我试图在cython中测试内存视图以更好地了解它们。在我的代码中,我将每个内存视图元素(如[1,2]
)作为cy
类元素move
传递。如何访问在cython中声明的类的类型化内存视图元素?
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'
。 是什么导致了这个错误,我该如何解决这个问题?
谢谢。我修正了错误。 –