如何理解 Julia 中的一些符号和语法?

how to understand few symbols and syntaxs in Julia?

我是 Julia 语言的新手,所以我开始阅读文档和所有内置函数。现在,我正在为我的工作学习一个 github 项目。由于我对 Python 更熟悉,所以我尝试根据自己的理解将 Julia 的代码翻译成 python,但是我得到了一些我不理解的奇怪语法,我被它们困住了。谁能指出这些语法的含义?提前致谢!

我不明白的语法

我不理解的那些 julia 代码行,因为我也没有在文档中找到它们。

var1 = Tuple{Integer, Vector}[]

这里我们声明了对象 var 1,有什么真实的例子吗? python 版本是什么?

还有如果X::Matrix, n::Int,那么下面的?是什么意思呢?我应该如何在 python 中编码?

K = [( i >= j ? dot(view(X,:,i), view(X,:,j)) : 0.0 )::Float64 for i=1:n, j=1:n]

我们应该如何在 python 中对此进行编码?

此外,我不确定下面->的含义:

for i=1:n 
      id_i = find(x -> x[1] == i, var1)
      xi_i_list =  map(x -> x[2], var1[id_i])

how should we translate this into python?

最后,我只是不明白下面.>的意思:

act= zeros(100)
alpha = zeros(10)

  for i=1:100
    idx = find(x::Tuple{Integer, Vector} -> x[1] == i, var1)
    act[i] = sum(alpha[idx] .> 1e-3)

作为新手,我正在尝试理解find()map()的作用。最好的情况是,我希望我可以用 Python 编写上面的 Julia 代码。但是我很难理解代码。谁能给出可能的解释和相应的 python 代码以供学习之用?提前致谢!

首先,Julia文档提供了Noteworthy differences from Python的列表。现在回答每个问题:

var1 = Tuple{Integer, Vector}[]

here we declare object var 1, what's a real example for that? what's the python version?

VectorArray{T,1} where T 的糖,表示具有任何类型元素的一维数组。 Tuple{Integer, Vector} 因此是一个带有 IntegerVector 的元组,例如 (1, [1, 2])var1 只是此类元组的空向量。 您可以 push! 像后者这样的元素变成 var1 来创建一个“真实”的例子:

julia> var1 = Tuple{Integer, Vector}[]
Tuple{Integer,Array{T,1} where T}[]

julia> push!(var1, (1, [1, 2]))
1-element Array{Tuple{Integer,Array{T,1} where T},1}:
 (1, [1, 2])

julia> push!(var1, (2, [3.0, "foo", 4]))
2-element Array{Tuple{Integer,Array{T,1} where T},1}:
 (1, [1, 2])
 (2, Any[3.0, "foo", 4])

what's the meaning of ?

您可以键入 ? 以访问 julia 中的“帮助”模式,然后询问它 ? 是什么。来自其文档:

a ? b : c

Short form for conditionals; read "if a, evaluate b otherwise evaluate c". Also known as the ternary operator.

This syntax is equivalent to if a; b else c end, but is often used to emphasize the value b-or-c which is being used as part of a larger expression, rather than the side effects that evaluating b or c may have.

See the manual section on control flow for more details.

Examples
julia> x = 1; y = 2;

julia> println(x > y ? "x is larger" : "y is larger")
y is larger

not sure about meaning of ->

这只是为了创建一个 anonymous function


I just don't understand the meaning of .>

这只是逐个元素的“大于”运算符 >。有关详细信息,请参阅 dotted operators 上的文档。