最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How to create a numpy.array from a list of floats with shared-memory with version 2.1.3 - Stack Overflow

programmeradmin3浏览0评论

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
  • 1 so, your example is not a list of floats (or ints), it's a list of lists. You should clarify exactly what you mean. But you can get the 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:12
  • 1 but a better question is, why do you want to do this? A dtype=object numpy.ndarray is basically a less performant python list that cannot change size – juanpa.arrivillaga Commented Mar 28 at 17:12
  • "I am aware of byearray, but that seems to only work for ints." no, a bytearray is a buffer of raw bytes, so you can use np.frombuffer, of course, you will always have to treat the original bytearray as a sequence of bytes, (although, you could use a memoryview 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
  • 1 I do not see any "shared memory" in your example. It looks like you talk about "object sharing" which is something different (and AFAIK possible). – Jérôme Richard Commented Mar 28 at 17:43
  • 1 Array operations on object dtype arrays are performed at essentially list comprehension speeds. There may be some notational convenience in converting lists to such arrays, but little speed benefits. – hpaulj Commented Mar 28 at 21:00
 |  Show 7 more comments

2 Answers 2

Reset to default 1

You 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.

发布评论

评论列表(0)

  1. 暂无评论