0

In python docs it describe the basic data structure type list at python's data structure. When I try to do work that breaks a list into two parts, and calls some functions with these parts. And calling some functions will modify the input list value but not space reallocate. Unfortunately, we can't break the list into different parts without reallocate space, which is different in c/c++ we use pointer to use the same space. It's there some way that we can reference the part of list like pointer in c/c++. Or only we can if we use other extends type in python, for example , numpy's array.

# here is the example
A = range(10)
part1_of_A = A[:5]
part2_of_A = A[5:]
part1_of_A[0] = 100 # but part1_of_A is copy but not reference of A
# A]0] not changed

# numpy array works
import numpy
A = numpy.zeros(10) 
part1_of_A = A[:5]
part2_of_A = A[5:]
part1_of_A[0] = 100  # it indeed change A[0]
# can list do the same job in other ways? Or it can't.
Shu Zhang
  • 19
  • 2
  • How is what you're asking different from using slices? True, I don't have a "pointer" to the subset, but a slice is still very simple to work with and leaves the data in place. – David Hoelzer Dec 18 '15 at 13:55
  • 2
    You can do these things with lists if you try hard enough (e.g. via ctypes), but it's far better not to use Python like it's C or C++. Lists are already highly optimised and there's almost certainly a better way to do things. – Alex Riley Dec 18 '15 at 13:57
  • 1) There is no language C/C++! 2) Don't add unrelated tags; just mentioning other languages does not make it relevant for these languages. 3) Why do you care about (re-)allocation in Python anyway? 4) Did you actually see any referance Python has a pointer type? – too honest for this site Dec 18 '15 at 14:03
  • 1
    @DavidHoelzer No, that doesn’t work. If you were for example to swap elements in a slice of a list, that would not propagate the swap to the original list. Slicing returns a new list, not a view on the list. Only numpy does views. And I think that’s the actual point of the question. – Jonas Schäfer Dec 18 '15 at 14:06
  • @Olaf I deleted the unrelated tags. If we have a list name A, then we do B=A, B[0]=100 change A[0], because B and A points to same memory space. In this way, I regard it as B reference A. Yes,in python we don't have pointer type, but can we get variable point to the memory of space slice of list. Such that B= A[1:5], B[0] =100 change A[1] – Shu Zhang Dec 18 '15 at 14:13
  • @JonasWielicki Thx. It helps a lot. – Shu Zhang Dec 18 '15 at 14:23
  • @ShuZhang: Python is referenced-based, so it is logical _in general_ `B` references the same list as `A` after the assignment. Note that `B` does not reference `A`, but **both reference the same object**. If, after the assignment, you have e.g. `A = 100`, `B` still references the original list, while `A` references an `int`. What you call "variables" here are actually names. They **always** are references to objects, be it a class, an instance of a class, a function (which is an instance of a class, too).; there is no other mechanism. References are not pointers! – too honest for this site Dec 18 '15 at 16:05
  • @JonasWielicki Ah, I missed that he wanted to actually manipulate the content of the slice. – David Hoelzer Dec 18 '15 at 16:19

0 Answers0