List reference at offset

Peter Otten __peter__ at web.de
Wed May 27 14:19:19 EDT 2009


Colin.Hankins at touit.com wrote:

> Disclaimer: I'm learning python and would like to use it in a
> project.
> 
> The project will have many large matricies. In one particular instance
> I need to reference a smaller submatrix of the larger matrix. I don't
> want to create a new copy or even change any of the values, I just
> want to quickly and easily be able to look at this sub matrix.
> 
> So I am using lists of lists for the matricies (I am also playing with
> NumPy) and is there anyway to create a pointer to an offset in a
> list?
> 
> I know you can have A = B = [] which would be the same.
> 
> And I know I can simply do a B[x + offset to submatrix].
> 
> But I was hoping there may be a way to just have a pointer? Or is
> there another more efficient method to do this in python?

What you describe is what numpy does:

"""
Therefore, a regular indexing expression on an ndarray can always produce an 
ndarray object without copying any data. This is sometimes referred to as 
the “view” feature of array indexing, and one can see that it is enabled by 
the use of striding information in the underlying ndarray object. The 
greatest benefit of this feature is that it allows indexing to be done
very rapidly and without exploding memory usage (because no copies of the 
data are made).
"""






More information about the Python-list mailing list