我對Python有點新(1天),所以也許我的問題很愚蠢。我已經看過here但我找不到我的答案。將數組/列表的子集引用作爲參數傳遞給Python
我需要以隨機大小隨機偏移修改數組的內容。 我有一個Python API來連接我無法修改的USB設備的DDL。還有一個功能,就像這樣:
def read_device(in_array):
# The DLL accesses the USB device, reads it into in_array of type array('B')
# in_array can be an int (the function will create an array with the int
# size and return it), or can be an array or, can be a tuple (array, int)
在我的代碼,我創建的,比方說,64個字節數組,我想讀從32rd字節開始的16個字節。在C中,我會給&my_64_array[31]
以read_device
函數。 在Python中,如果給定:
read_device(my_64_array[31:31+16])
似乎in_array是給定的子集的副本的引用,因此my_64_array
不被修改。
我該怎麼辦?我必須拆分my_64_array
並在?之後重新組合?
謝謝你的回答。正如我對@sapht所說的,我無法修改API函數。我想過切片和重組後,但我不確定內存效率。例如:a = [0,1,2,3,4] –
@MichelL哎呀!我明白...戴着帽子。它是否返回數組? –
是的,原型是 **(int return,u08 [] data_in,u16 num_read)= read_device(Device handle,u08 [] data_in)** –