[英]Julia:How to access an element in an array of a type based on the type field with specific value
我有一個自定義類型的數組,稱為links
,其元素的類型為Link
。
type Link
first::Int64
second::Int64
value::Array{Float64,1}
end
,此外,對於links
, typeof(links)
是一個Vector{Link}
。
您可能已經猜到了,這是我所擁有的圖形定義的一部分,其中包括邊線, first
指的是一個端點, second
指的是另一個端點。 我想要做的就是選擇value
一的link
在links
在端點first
等於特定的節點數量,讓我們稱之為vertex_id
。 簡而言之,我想要以下內容:
value of all those in links, whose .first == vertex_id
。
PS,我知道對於常規類型的DataFrame,我可以說
df[df[:col1] .== x,:col2]
但是,對於自定義類型的數組,有沒有類似的方法呢?
.
使用以下方法廣播語法: getfield
將是另一個選擇(也許更類似於您對DataFrames所做的操作):
getfield.(links,[:value])[getfield.(links, [:first]).==vertex_id]
但是您建議的列表理解解決方案可能更優雅。
[x.value for x in links if x.first == vertex_id]
我意識到我可以理解:
[x.value for x in links if x.first == vertex_id]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.