Vectorized “in” function in julia?

前端 未结 5 1246
抹茶落季
抹茶落季 2020-11-28 13:53

I often want to loop over a long array or column of a dataframe, and for each item, see if it is a member of another array. Rather than doing

giant_list =          


        
5条回答
  •  无人及你
    2020-11-28 14:23

    There are a handful of modern (i.e. Julia v1.0) solutions to this problem:

    First, an update to the scalar strategy. Rather than using a 1-element tuple or array, scalar broadcasting can be achieved using a Ref object:

    julia> in.(giant_list, Ref(good_letters))
    3-element BitArray{1}:
      true
     false
     false
    

    This same result can be achieved by broadcasting the infix (\inTAB) operator:

    julia> giant_list .∈ Ref(good_letters)
    3-element BitArray{1}:
      true
     false
     false
    

    Additionally, calling in with one argument creates a Base.Fix2, which may later be applied via a broadcasted call. This seems to have limited benefits compared to simply defining a function, though.

    julia> is_good1 = in(good_letters);
           is_good2(x) = x in good_letters;
    
    julia> is_good1.(giant_list)
    3-element BitArray{1}:
      true
     false
     false
    
    julia> is_good2.(giant_list)
    3-element BitArray{1}:
      true
     false
     false
    

    All in all, using .∈ with a Ref will probably lead to the shortest, cleanest code.

提交回复
热议问题