The (incorrect) idea that Python's "lists" are not contiguous memory arrays seems to be somewhat widespread. I'm not sure where it comes from – maybe they used have a different implementation, or maybe its just the unfortunate insistence on calling them lists rather than arrays.
On Tue, Aug 5, 2014 at 11:57 PM, John Myles White <[email protected]> wrote: > For future reference, Jason’s interpretation of Python lists was > completely accurate: > https://docs.python.org/2/faq/design.html#how-are-lists-implemented > > — John > > On Aug 5, 2014, at 4:08 AM, Neal Becker <[email protected]> wrote: > > > Thanks. I don't think that's really equivalent though. I believe > python's list > > is not contiguous, and can efficiently handle insertions and deletions, > both at > > the ends and in the middle. Julia's array is a contiguous dense array, > correct? > > > > Jason Merrill wrote: > > > >> julia> {} > >> 0-element Array{Any,1} > >> > >> is like python's arrays in that it can hold values of heterogenous > types, > >> and you can grow it with push!, e.g. > >> > >> julia> a = {} > >> 0-element Array{Any,1} > >> > >> julia> push!(a, "grapes") > >> 1-element Array{Any,1}: > >> "grapes" > >> > >> julia> push!(a, 3) > >> 2-element Array{Any,1}: > >> "grapes" > >> 3 > >> > >> If you know the type of all the things you'll be putting in your > container, > >> you will get better performance/memory efficiency with a typed array. > >> > >> julia> b = Float64[] > >> 0-element Array{Float64,1} > >> > >> julia> push!(b, 1.0) > >> 1-element Array{Float64,1}: > >> 1.0 > >> > >> julia> push!(b, NaN) > >> 2-element Array{Float64,1}: > >> 1.0 > >> NaN > >> > >> I think in either case, Julia is smart about growing the array by the > right > >> amount at the right time to get O(1) ammortized push!(), but if you > have a > >> guess about the eventual size of your array, you can help Julia out with > >> `sizehint`. > >> > >> julia> sizehint(b, 100000) > >> > >> You don't have to be exactly right with sizehint--the worst that will > >> happen is that you will use a little more memory than you needed to, or > the > >> array will have to be dynamically grown more often than it had to be. > For > >> this, I am most grateful. In Matlab, your choices are, AFAICT, 1) know > the > >> exact size of your container correctly ahead of time, 2) have terrible > >> performance, or 3) allocate something that's definitely bigger than you > >> need, and then keep track of how much of it you've used yourself and > throw > >> away the excess at the end. > >> > >> > >> On Monday, August 4, 2014 4:16:27 PM UTC-7, Neal Becker wrote: > >>> > >>> What would be similar to python list type? A container that has an > >>> efficient > >>> append. A common use is construct an empty list, then grow it by > calling > >>> append. > >>> > >>> > > > > > >
