0
import numpy
pre_list = [1, 2, 3, 4]
post_array = numpy.array(pre_list)
post_array[2] = 10
print(pre_list)

I notice that [1, 2, 3, 4] is printed, which means pre_list was not modified. I guess numpy copied each elements of pre_list, and it may cost time cn, where n is len(pre_list), c is a constant. Can I create an array as a view onto a native list, so that they can share elements?

I want to do so, because a list is given, I hope to implement an algorithm on it in a time complexity logn, but either use method functions of ndarray for convince.

achedeuzot
  • 4,164
  • 4
  • 41
  • 56
Ricky Gao
  • 1
  • 1
  • 1
    The real question is why would you want to do so? Everything you can do with python `list`, you can do with `np.array`. So why don't you just work with a `np.array` from scratch? – Julien Nov 01 '17 at 01:40
  • Possible duplicate of [View onto a numpy array?](https://stackoverflow.com/questions/4370745/view-onto-a-numpy-array) – Erlinska Nov 01 '17 at 01:47
  • The underlying structure of arrays and lists are too different. A list contains pointers to objects elsewhere. In this case 4 pointers to unique integers (in cPython integers up to 255 are unique). The array reads the list, and constructs its own data buffer. In this case its databuffer is 16 bytes (4x4). `view` is a numpy concept, not a list one. – hpaulj Nov 01 '17 at 01:47

0 Answers0