gpt4 book ai didi

julia - 在 Julia 中过滤字典

转载 作者:行者123 更新时间:2023-12-05 01:03:44 26 4
gpt4 key购买 nike

我想使用 filter() 函数过滤字典,但我遇到了麻烦。我希望完成的是,返回值的某些条件的键。但是我得到一个方法错误

using Agents: AbstractAgent

# Define types
mutable struct Casualty <: AbstractAgent
id::Int
ts::Int
rescued::Bool

function Casualty(id,ts; rescued = false)
new(id,ts,rescued)
end
end

mutable struct Rescuer <: AbstractAgent
id::Int
actions::Int
dist::Float64

function Rescuer(id; action = rand(1:3) , dist = rand(1)[1])
new(id,action,dist)
end
end

cas1 = Casualty(1,2)
cas2 = Casualty(2,3)
resc1 = Rescuer(3)

agents = Dict(1=> cas1, 2 => cas2, 3 => resc1)

现在过滤

filter((k,v) -> v isa Casualty, agents)

# ERROR: MethodError: no method matching (::var"#22#23")(::Pair{Int64, AbstractAgent})

# what I truly wish to achieve is return the key for some condition of the value

filter((k,v) -> k ? v isa Casualty : "pass", agents)
# ofcourse I am not sure how to "pass" using this format

知道如何实现这一点。谢谢

最佳答案

对于字典,filter 得到一个键值对,也可以这样做(解构 Pair):

julia> dict = Dict(1=>"a", 2=>"b", 3=>"c")
Dict{Int64, String} with 3 entries:
2 => "b"
3 => "c"
1 => "a"

julia> filter(((k,v),) -> k == 1 || v == "c", dict)
Dict{Int64, String} with 2 entries:
3 => "c"
1 => "a"

或例如(将Pair作为一个整体):

julia> filter(p -> first(p) == 1 || last(p) == "c", dict)
Dict{Int64, String} with 2 entries:
3 => "c"
1 => "a"

julia> filter(p -> p[1] == 1 || p[2] == "c", dict)
Dict{Int64, String} with 2 entries:
3 => "c"
1 => "a"

编辑

解释为什么需要额外的括号:

julia> f = (x, y) -> (x, y)
#1 (generic function with 1 method)

julia> g = ((x, y),) -> (x, y)
#3 (generic function with 1 method)

julia> methods(f)
# 1 method for anonymous function "#1":
[1] (::var"#1#2")(x, y) in Main at REPL[1]:1

julia> methods(g)
# 1 method for anonymous function "#3":
[1] (::var"#3#4")(::Any) in Main at REPL[2]:1

julia> f(1, 2)
(1, 2)

julia> f((1, 2))
ERROR: MethodError: no method matching (::var"#1#2")(::Tuple{Int64, Int64})
Closest candidates are:
(::var"#1#2")(::Any, ::Any) at REPL[1]:1

julia> g(1, 2)
ERROR: MethodError: no method matching (::var"#3#4")(::Int64, ::Int64)
Closest candidates are:
(::var"#3#4")(::Any) at REPL[2]:1

julia> g((1, 2))
(1, 2)

如您所见,f 采用 2 个位置参数,而 g 采用一个被解构的位置参数(即假设参数传递给 g 是可迭代的,并且至少有 2 个元素)。另见 https://docs.julialang.org/en/v1/manual/functions/#Argument-destructuring .

现在是棘手的部分:

julia> h1((x, y)) = (x, y)
h1 (generic function with 1 method)

julia> methods(h1)
# 1 method for generic function "h1":
[1] h1(::Any) in Main at REPL[1]:1

julia> h2 = ((x, y)) -> (x, y)
#1 (generic function with 1 method)

julia> methods(h2)
# 1 method for anonymous function "#1":
[1] (::var"#1#2")(x, y) in Main at REPL[3]:1

在本例中,h1 是一个命名函数。在这种情况下,只需将参数包装在额外的括号中即可获得解构行为。对于匿名函数,由于 Julia 解析器的工作方式,需要额外的 , - 如果省略它,额外的括号将被忽略。

现在让我们检查 filter 文档字符串:

filter(f, d::AbstractDict)

Return a copy of d, removing elements for which f is false.The function f is passed key=>value pairs.

从这个文档字符串中可以看出,f 传递了一个参数,即 Pair。这就是为什么您需要使用解构或定义单个参数函数并在函数中提取其元素的原因。

关于julia - 在 Julia 中过滤字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73589207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com