【问题标题】:ERROR: MethodError: 'length' has no method matching length(::Filter{Function,Tuple{...}})错误:MethodError:'length' 没有匹配长度的方法(::Filter{Function,Tuple{...}})
【发布时间】:2016-02-19 08:16:42
【问题描述】:

我在here 中读到你不能filter! 一个元组,因为它是不可变的,但你可以filter 它。那么,为什么这会给我错误?

a = tuple([collect(1:10)]...)
b = [x for x in filter(y -> y % 2 == 0, a)]
ERROR: MethodError: `length` has no method matching length(::Filter{Function,Tuple{Int64}})

【问题讨论】:

    标签: filter tuples list-comprehension julia


    【解决方案1】:

    并不是你不能过滤它,而是length(由列表理解调用)没有方法(至少)对于filter返回的东西,这是一个可迭代但输入Filter。您可以将过滤器包裹在collect 中,或者更好的是,简化整个操作:

    a = tuple(collect(1:10)...)
    b1 = [x for x in collect(filter(y -> y % 2 == 0, a))]
    b2 = collect(filter(y -> y % 2 == 0, a))
    

    【讨论】:

    • 过滤器迭代器不能有长度,您可以查看示例 ``filter(x->rand()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2019-09-29
    • 2016-06-04
    • 1970-01-01
    相关资源
    最近更新 更多