admin管理员组文章数量:1356845
I am trying to create a numpy array from a list of floats with shared-memory such that updating the list updates the numpy array.
import numpy as np
lists = [[1]]
arr = np.asarray(lists, dtype=object)
lists[0][0] = 9999
arr # [[1]], but I want to return 9999
If I add copy=False, I get the error: ValueError: Unable to avoid copy while creating an array as requested.
I am aware of byearray, but that seems to only work for ints. Is there any possible way to have the a numpy array of list of numpy floats without creating a copy?
I am trying to create a numpy array from a list of floats with shared-memory such that updating the list updates the numpy array.
import numpy as np
lists = [[1]]
arr = np.asarray(lists, dtype=object)
lists[0][0] = 9999
arr # [[1]], but I want to return 9999
If I add copy=False, I get the error: ValueError: Unable to avoid copy while creating an array as requested.
I am aware of byearray, but that seems to only work for ints. Is there any possible way to have the a numpy array of list of numpy floats without creating a copy?
Share Improve this question edited Mar 28 at 17:18 juanpa.arrivillaga 96.5k14 gold badges138 silver badges186 bronze badges asked Mar 28 at 16:59 qq_rr_ii_mmqq_rr_ii_mm 231 silver badge2 bronze badges 12 | Show 7 more comments2 Answers
Reset to default 1You have created a list inside list and converted into numpy array and expecting to change the value of the inner list without copying. This doesn't work this way. Instead you can convert the inner list to numpy array and place it inside a normal python list.
import numpy as np
# Create a list containing a numpy array
inner_arr = np.array([1.0], dtype=np.float64)
lists = [inner_arr]
# Create the outer array
arr = np.array(lists, dtype=object)
# Now modify the inner array
lists[0][0] = 9999
print(arr) # output: [array([9999.])]
If your list contains mutable objects, you can make an object dtype array from it, and modify those objects. But can't replace them in one, and see the change in the other.
In [423]: alist = [[1,foo,'astr'],[(1,2,3),[4,5,6],3.4]]
In [424]: arr = np.array(alist, object)
In [425]: alist
Out[425]: [[1, <function __main__.foo(a, b_list)>, 'astr'], [(1, 2, 3), [4, 5, 6], 3.4]]
In [426]: arr
Out[426]:
array([[1, <function foo at 0x000001595F1CBF60>, 'astr'],
[(1, 2, 3), list([4, 5, 6]), 3.4]], dtype=object)
Modifying an element of the list:
In [427]: arr[1,1]
Out[427]: [4, 5, 6]
In [428]: arr[1,1][1]=500
In [429]: arr
Out[429]:
array([[1, <function foo at 0x000001595F1CBF60>, 'astr'],
[(1, 2, 3), list([4, 500, 6]), 3.4]], dtype=object)
In [430]: alist
Out[430]:
[[1, <function __main__.foo(a, b_list)>, 'astr'],
[(1, 2, 3), [4, 500, 6], 3.4]]
The tuple object cnn't be changed:
In [431]: arr[1,0][1]=2.3
TypeError: 'tuple' object does not support item assignment
Other list changes
In [432]: alist[1][1][0]=20; alist
Out[432]:
[[1, <function __main__.foo(a, b_list)>, 'astr'],
[(1, 2, 3), [20, 500, 6], 3.4]]
In [435]: arr[1][1][2]=30
In [438]: arr[1][1].append(10); alist
Out[438]:
[[1, <function __main__.foo(a, b_list)>, 'astr'],
[(1, 2, 3), [20, 500, 30, 10], 3.4]]
I included [435] to show that the [1][1]
syntax (as opposed to [1,1]
) is ok, since we are modifying the shared list element.
版权声明:本文标题:python - How to create a numpy.array from a list of floats with shared-memory with version 2.1.3 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744023415a2577624.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
dtype=object
array to refer to the same objects as the lists, but the outer list will never share the same buffer as the numpy array (without some serious hacky stuff) – juanpa.arrivillaga Commented Mar 28 at 17:12dtype=object
numpy.ndarray is basically a less performant python list that cannot change size – juanpa.arrivillaga Commented Mar 28 at 17:12bytearray
is a buffer of raw bytes, so you can usenp.frombuffer
, of course, you will always have to treat the original bytearray as a sequence of bytes, (although, you could use amemoryview
to make this easier to handle!) But you should elaborate on what exactly you are trying to accomplish so we can better understand what might help – juanpa.arrivillaga Commented Mar 28 at 17:29