我试图理解 Julia 如何复制和处理变量。看一下以下示例和以下问题:
a = 1
b = 1
a === b #why do they share the same address? I defined them independently
a = 1
b = a
a === b #true, this makes sense
a = 10 #b still has value of 1 though? why is that when they share the same address as per above?
a = (1,2)
b = (1,2)
a === b #how can they have the same address?
a = [1,2]
b = [1,2] …Run Code Online (Sandbox Code Playgroud) 我有以下变量
a1 = 2
a2 = 20
a3 = 200
Run Code Online (Sandbox Code Playgroud)
是否可以在循环整数 1、2 和 3 时输出它们?类似下面的东西,尽管它没有按预期工作
for i in [1,2,3]
println(:"a$i") # doesn't work
println("a" * string(i)) # doesn't work
end
Run Code Online (Sandbox Code Playgroud) 我想循环变量列表并输出变量名称和值。例如,假设我有x=1和y=2,那么我想要一个输出
x is 1
y is 2
Run Code Online (Sandbox Code Playgroud)
我怀疑我需要为此使用符号。这是我的方法,但它不起作用:
function t(x,y)
for i in [x,y]
println("$(Symbol(i)) is $(eval(i))") # outputs "1 is 1" and "2 is 2"
end
end
t(1, 2)
Run Code Online (Sandbox Code Playgroud)
有办法实现这一点吗?我想字典会起作用,但有兴趣看看符号是否也可以在这里使用。