小编sto*_*ner的帖子

根据 Julia 中的另一个列表查找列表索引

我有两个列表,A和B,B是A的子列表。A和B中的元素都是随机排序的。我正在寻找一种方法来查找 B 中元素的 A 中元素索引,可能是有序的。在Python中,以下内容似乎有效

B_list = sorted([A.index(i) for i in B])
Run Code Online (Sandbox Code Playgroud)

我不确定在 Julia 中如何实现这一点。我对这门语言很陌生。我尝试过类似的东西

B_list = sort(filter(in(B), A))
Run Code Online (Sandbox Code Playgroud)

完全没有效果,请帮忙!

list julia

4
推荐指数
1
解决办法
290
查看次数

Julia 中多个条件的三元运算符

我有 Julia 代码,如图所示

if cxp1_v1 < cxp1_v2 
    d_min = cxp1_v1 
    d_max = cxp1_v2 
else 
    d_min = cxp1_v2
    d_max = cxp1_v1 
end
Run Code Online (Sandbox Code Playgroud)

或者

if cxp1_v1 < cxp1_v2 d_min, d_max = cxp1_v1, cxp1_v2 else d_min,  d_max  = cxp1_v2, cxp1_v1 end
Run Code Online (Sandbox Code Playgroud)

有没有办法在三元运算符中完成同样的任务?我尝试了如图所示的东西

cxp1_v1 < cxp1_v2 ? d_min, d_max = cxp1_v1, cxp1_v2:d_min,  d_max  = cxp1_v2, cxp1_v1
Run Code Online (Sandbox Code Playgroud)

但我得到

syntax: colon expected in "?" expression

Stacktrace:
 [1] top-level scope
   @ In[422]:11
 [2] eval
   @ ./boot.jl:373 [inlined]
 [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1196
Run Code Online (Sandbox Code Playgroud)

conditional-operator julia

3
推荐指数
1
解决办法
206
查看次数

在 Julia 中检查矩阵是否为零矩阵的有效方法

我有一个 n×n 矩阵(n 非常大),我想检查它在 Julia 中是否为零矩阵。我尝试的方法之一如下

using BenchmarkTools
using Random

A = rand(2000,2000); # n = 2000 for instance.
@btime all($A .== 0) 
Run Code Online (Sandbox Code Playgroud)

其输出到

2.380 ms (4 allocations: 492.59 KiB)
false
Run Code Online (Sandbox Code Playgroud)

还有其他有效的方法可以达到同样的目的吗?

arrays julia

2
推荐指数
1
解决办法
909
查看次数

Julia 中的结构数组排序

假设我在 Julia 中有以下内容:

mutable struct emptys
    begin_time::Dict{Float64,Float64}; finish_time::Dict{Float64,Float64}; Revenue::Float64
end
population = [emptys(Dict(),Dict(),-Inf) for i in 1:n_pop] #n_pop is a large positive integer value.
for ind in 1:n_pop
    r = rand()
    append!(population[ind].Revenue, r)
    append!(population[ind].begin_time, Dict(r=>cld(r^2,rand())))
    append!(population[ind].finish_time, Dict(r=>r^3/rand()))
 end 
Run Code Online (Sandbox Code Playgroud)

现在我想根据收入值对总体进行排序。Julia 有什么办法可以实现这一目标吗?如果我用 Python 来做的话,会是这样的:

sorted(population, key = lambda x: x.Revenue) # The population in Python can be prepared using https://pypi.org/project/ypstruct/ library. 
Run Code Online (Sandbox Code Playgroud)

请帮忙。

sorting struct julia

1
推荐指数
1
解决办法
514
查看次数

标签 统计

julia ×4

arrays ×1

conditional-operator ×1

list ×1

sorting ×1

struct ×1