Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

findin does not work for strings? #11630

Closed
weijianzhg opened this issue Jun 9, 2015 · 8 comments
Closed

findin does not work for strings? #11630

weijianzhg opened this issue Jun 9, 2015 · 8 comments

Comments

@weijianzhg
Copy link

I am using Julia Version 0.4.0-dev+5149 (2015-06-01 18:58 UTC).
Suppose I have a list l

 julia> l = ["a", "a", "b", "c"]
 4-element Array{ASCIIString,1}:
 "a"
 "a"
 "b"
 "c"

then findfirst and findlast can find the correct index.

julia> findfirst(l, "a")
1
julia> findlast(l, "a")
2

But findin(l, "a") returns nothing

julia> findin(l, "a")
0-element Array{Int64,1}

Is this a bug or I misunderstand something?

Thanks,

Weijian

@KristofferC
Copy link
Sponsor Member

This is my interpretation from looking at the documentation:

findin(a, b)
Returns the indices of elements in collection a that appear in collection b
julia> findin(l, "a")
0-element Array{Int64,1}

julia> findin(l, ["a"])
2-element Array{Int64,1}:
 1
 2

@weijianzhg
Copy link
Author

I see. Thanks @KristofferC , I think I misuse it. But it does return the correct result for integers.

julia> findin([1,2,3,3,4], 3)
2-element Array{Int64,1}:
 3
 4

@KristofferC
Copy link
Sponsor Member

Ok so this is the definition of findin

function findin(a, b)
    ind = Array(Int, 0)
    bset = Set(b)
    @inbounds for i = 1:length(a)
        a[i] in bset && push!(ind, i)
    end
    ind
end

The problem is that for a string b, the command Set(b) produces:

julia> Set("a")
Set(['a'])

julia> typeof(Set("a"))
Set{Char}

and thus later in the function this fails:

julia> "a" in Set("a")
false

On the other hand, these are true

julia> "a" in Set(["a"])
true

julia> 'a' in Set(["a"])
true

I am a bit surprised that Set(::ASCIIString) returns a set of Char. I guess since Set(Container{T}) should return a Set{T} and a string technically works as a container for Char it makes sense.

Maybe you should reopen this to confirm that this is what is expected.

@weijianzhg weijianzhg reopened this Jun 9, 2015
@nalimilan
Copy link
Member

String is indeed a collection of Chars, since that's what you get when indexing a string. If String wasn't a collection but a scalar, you would get an error.

If you want to look for strings in a string (as opposed to Charss in a string, as here), you can use search instead. See #10593 about attempts at making this more obivous.

@KristofferC
Copy link
Sponsor Member

@nalimilan: "If String wasn't a collection but a scalar, you would get an error."

This doesn't seem to be the case.

julia> findin([1,2,3,3,4], 3)
2-element Array{Int64,1}:
 3
 4

Because set of a scalar promotes it to a container with the scalar as the only item?

@nalimilan
Copy link
Member

No, that's because numbers are considered as collections at the moment:

julia> 2[1]
2

I find it a bit confusing, for people have advocated that it was often useful.

@KristofferC
Copy link
Sponsor Member

Oh, I see. Thanks.

@weijianzhg
Copy link
Author

Thanks @nalimilan and @KristofferC for clarifying this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants