在 Julia 中复制或克隆集合 [英] Copy or clone a collection in Julia

查看:12
本文介绍了在 Julia 中复制或克隆集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Julia中创建了一个一维数组(向量),即a=[1, 2, 3, 4, 5].然后我想创建一个新的向量b,其中ba中有完全相同的元素,即b=[1, 2, 3, 4, 5].

I have created a one-dimensional array(vector) in Julia, namely, a=[1, 2, 3, 4, 5]. Then I want to create a new vector b, where b has exactly same elements in a, i.e b=[1, 2, 3, 4, 5].

似乎直接使用b = a只是为原始集合创建一个指针,这意味着如果我修改ba是可变的,修改也会反映在 a 中.例如,如果我使用 !pop(b),那么 b=[1, 2, 3, 4]a=[1, 2, 3, 4].

It seems that directly use b = a just create a pointer for the original collection, which means if I modify b and a is mutable, the modification will also be reflected in a. For example, if I use !pop(b), then b=[1, 2, 3, 4] and a=[1, 2, 3, 4].

我想知道是否有一个仅复制或克隆集合的官方功能,b中的更改不会发生在a中.我发现一个解决方案是使用 b = collect(a).如果有人提供其他方法,我将不胜感激.

I am wondering if there is a official function to merely copy or clone the collection, which the change in b will not happen in a. I find a solution is use b = collect(a). I would appreciate that someone provide some other approaches.

推荐答案

b=copy(a)

应该做你想做的.

methods(copy) 将为您提供 copy 的方法列表,它会告诉您这将适用于哪些类型的 a.

methods(copy) will give you a list of methods for copy, which will tell you what types of a this will work for.

julia> methods(copy)
# 32 methods for generic function "copy":
copy(r::Range{T}) at range.jl:324
copy(e::Expr) at expr.jl:34
copy(s::SymbolNode) at expr.jl:38
copy(x::Union{AbstractString,DataType,Function,LambdaStaticData,Number,QuoteNode,Symbol,TopNode,Tuple,Union}) at operators.jl:194
copy(V::SubArray{T,N,P<:AbstractArray{T,N},I<:Tuple{Vararg{Union{AbstractArray{T,1},Colon,Int64}}},LD}) at subarray.jl:29
copy(a::Array{T,N}) at array.jl:100
copy(M::SymTridiagonal{T}) at linalg/tridiag.jl:63
copy(M::Tridiagonal{T}) at linalg/tridiag.jl:320
copy{T,S}(A::LowerTriangular{T,S}) at linalg/triangular.jl:36
copy{T,S}(A::Base.LinAlg.UnitLowerTriangular{T,S}) at linalg/triangular.jl:36
copy{T,S}(A::UpperTriangular{T,S}) at linalg/triangular.jl:36
copy{T,S}(A::Base.LinAlg.UnitUpperTriangular{T,S}) at linalg/triangular.jl:36
copy{T,S}(A::Symmetric{T,S}) at linalg/symmetric.jl:38
copy{T,S}(A::Hermitian{T,S}) at linalg/symmetric.jl:39
copy(M::Bidiagonal{T}) at linalg/bidiag.jl:113
copy(S::SparseMatrixCSC{Tv,Ti<:Integer}) at sparse/sparsematrix.jl:184
copy{Tv<:Float64}(A::Base.SparseMatrix.CHOLMOD.Sparse{Tv<:Float64}, stype::Integer, mode::Integer) at sparse/cholmod.jl:583
copy(A::Base.SparseMatrix.CHOLMOD.Dense{T<:Union{Complex{Float64},Float64}}) at sparse/cholmod.jl:1068
copy(A::Base.SparseMatrix.CHOLMOD.Sparse{Tv<:Union{Complex{Float64},Float64}}) at sparse/cholmod.jl:1069
copy(a::AbstractArray{T,N}) at abstractarray.jl:349
copy(s::IntSet) at intset.jl:34
copy(o::ObjectIdDict) at dict.jl:358
copy(d::Dict{K,V}) at dict.jl:414
copy(a::Associative{K,V}) at dict.jl:204
copy(s::Set{T}) at set.jl:35
copy(b::Base.AbstractIOBuffer{T<:AbstractArray{UInt8,1}}) at iobuffer.jl:38
copy(r::Regex) at regex.jl:65
copy(::Base.DevNullStream) at process.jl:98
copy(C::Base.LinAlg.Cholesky{T,S<:AbstractArray{T,2}}) at linalg/cholesky.jl:160
copy(C::Base.LinAlg.CholeskyPivoted{T,S<:AbstractArray{T,2}}) at linalg/cholesky.jl:161
copy(J::UniformScaling{T<:Number}) at linalg/uniformscaling.jl:17
copy(A::Base.SparseMatrix.CHOLMOD.Factor{Tv}) at sparse/cholmod.jl:1070

这篇关于在 Julia 中复制或克隆集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆