# Questions of vector

**URL:** https://discourse.julialang.org/t/questions-of-vector/87652
**Category:** New to Julia
**Tags:** question, arrays
**Created:** [September 22, 2022, 2:32pm UTC](https://discourse.julialang.org/t/questions-of-vector/87652 "2022-09-22T14:32:00Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![zhangchunyong](https://avatars.discourse-cdn.com/v4/letter/z/d9b06d/32.png) [@zhangchunyong](https://discourse.julialang.org/u/zhangchunyong)
#### Post date: [September 22, 2022, 2:32pm UTC](https://discourse.julialang.org/t/questions-of-vector/87652/1 "2022-09-22T14:32:00Z")

</div>

```julia
julia> a=[(1,1),(1,2),(1,3),(1,4),(2,1),(2,2)]
6-element Vector{Tuple{Int64, Int64}}:
 (1, 1)
 (1, 2)
 (1, 3)
 (1, 4)
 (2, 1)
 (2, 2)

```

There is a vector called a.If i input 1,I want to return [1,2,3.4].Also if I input 2,I want to return [1,2].Thanks for helping me.  
And I also want to know the index of (1,4) in a.I wrote findfirst((x,y)-\>x==1&y==4,a),but it didn’t work.  
Would you please to help me.Thanks.

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [September 22, 2022, 2:48pm UTC](https://discourse.julialang.org/t/questions-of-vector/87652/2 "2022-09-22T14:48:18Z")

</div>

`findall(x -> first(x) == 1, a)`

gives the desired indices

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [September 22, 2022, 2:48pm UTC](https://discourse.julialang.org/t/questions-of-vector/87652/3 "2022-09-22T14:48:58Z")

</div>

If possible, it’s better to store this as a `Dict`: `a = Dict(1 => [1, 2, 3, 4], 2 => [1, 2])`. Of course, if the values are actually `1` and `2` (and similar consecutive small numbers), it’s even better to use arrays: `a = [[1, 2, 3, 4], [1, 2]]`.

If you already have them as a vector of tuples for some other reason and would like to keep it so, you can do:

```julia
julia> getvalues(tuplearray, key) = last.(filter(t -> t[1] == key, tuplearray))
getvalues (generic function with 1 method)

julia> getvalues(a, 1)
4-element Vector{Int64}:
 1
 2
 3
 4

```

and

```julia
julia> findfirst(==((1, 4)), a)
4

```

(Note the double brackets on `==((1, 4))` - one pair for the function call `==()`, another to define it as a tuple `(1, 4)`.)

---

<div class="post-metadata">

### Author: ![zhangchunyong](https://avatars.discourse-cdn.com/v4/letter/z/d9b06d/32.png) [@zhangchunyong](https://discourse.julialang.org/u/zhangchunyong)
#### Post date: [September 22, 2022, 3:04pm UTC](https://discourse.julialang.org/t/questions-of-vector/87652/5 "2022-09-22T15:04:29Z")

</div>

Thanks.If I want to know the index of (1,2) and (1,4),and return a vector. I can only think of one solution that I create a vector, and push it one by one like the following.

```julia
q=Int64[]
w=findfirst(==((1, 2)), a)
push!(q,w)
e=findfirst(==((1, 4)), a)
push!(q,e)

```

Is there a alternative way which is faster than this?I think push! is slow.

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [September 22, 2022, 3:06pm UTC](https://discourse.julialang.org/t/questions-of-vector/87652/6 "2022-09-22T15:06:29Z")

</div>

`push!` shouldn’t be that slow. Julia doesn’t allocate on every `push!` call.

Maybe you want `findall`?

```julia
julia> findall(a) do x
           x == (1, 1) ||
           x == (1, 4)
       end
2-element Vector{Int64}:
 1
 4

```

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [September 22, 2022, 3:22pm UTC](https://discourse.julialang.org/t/questions-of-vector/87652/7 "2022-09-22T15:22:16Z")

</div>

I was going to suggest

```julia
julia> findall(in(((1, 4), (1, 2))), a)
2-element Vector{Int64}:
 2
 4

```

but @pdeffebach’s way is probably clearer.

If you’re going to be doing a lot of such find operations, then `sort`ing your array and then using `searchsorted` would likely be even faster. In this example the array is already sorted, so:

```julia
julia> vcat((collect(searchsorted(a, t)) for t in ((1, 4), (1, 2)))...)
2-element Vector{Int64}:
 4
 2

```

(Though the code does get a bit messy like above due to the way `searchsorted`’s return value works.)

---

<div class="post-metadata">

### Author: ![zhangchunyong](https://avatars.discourse-cdn.com/v4/letter/z/d9b06d/32.png) [@zhangchunyong](https://discourse.julialang.org/u/zhangchunyong)
#### Post date: [September 22, 2022, 3:44pm UTC](https://discourse.julialang.org/t/questions-of-vector/87652/8 "2022-09-22T15:44:04Z")

</div>

[quote=“pdeffebach, post:6, topic:87652”]  
Maybe you want `findall`?  
What I want to do is that I make a for loop,and push! it to the vector.And finally ,my aim is to delete them.I use deleteat!.I can delete one in each for.or delete at last.

```julia
a=[(1,1),(1,2),(1,3),(1,4),(2,1),(2,2)]
q=Int64[]
for m in a
    if isodd(m[1]+m[2])
        push!(q,findfirst(==((m[1], m[2])), a))
    end
end
julia> deleteat!(a,q)
3-element Vector{Tuple{Int64, Int64}}:
 (1, 1)
 (1, 3)
 (2, 2)

```

or the next one

```julia
a=[(1,1),(1,2),(1,3),(1,4),(2,1),(2,2)]
for m in a
    if isodd(m[1]+m[2])
    deleteat!(a,findfirst(==((m[1], m[2])), a))
    end
end 

```

If I want to delete many , and i will push many times like the first code and finally delete.Or delete one by one.Are there any alternative ways like the first one in for loop but do not use push! to generate a vector.Thanks

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [September 22, 2022, 3:47pm UTC](https://discourse.julialang.org/t/questions-of-vector/87652/9 "2022-09-22T15:47:34Z")

</div>

You might be interested in [SplitApplyCombine.jl](https://github.com/JuliaData/SplitApplyCombine.jl) for this kind of thing.

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [September 22, 2022, 3:55pm UTC](https://discourse.julialang.org/t/questions-of-vector/87652/10 "2022-09-22T15:55:38Z")

</div>

> [@zhangchunyong](#):
>
> ```julia
> a=[(1,1),(1,2),(1,3),(1,4),(2,1),(2,2)]
> q=Int64[]
> for m in a
> if isodd(m[1]+m[2])
> push!(q,findfirst(==((m[1], m[2])), a))
> end
> end
> julia> deleteat!(a,q)
> 3-element Vector{Tuple{Int64, Int64}}:
> (1, 1)
> (1, 3)
> (2, 2)
> 
> ```

```julia
julia> filter!(a) do t
         !isodd(t[1] + t[2])
       end
3-element Vector{Tuple{Int64, Int64}}:
 (1, 1)
 (1, 3)
 (2, 2)

```

---

<div class="post-metadata">

### Author: ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)
#### Post date: [September 22, 2022, 3:58pm UTC](https://discourse.julialang.org/t/questions-of-vector/87652/11 "2022-09-22T15:58:56Z")

</div>

```julia
a = [(1,1),(1,2),(1,3),(1,4),(2,1),(2,2)]
r = [element[2] for (indx, element) in pairs(a) if element[1] == 1] # first question
a = [i for i in a if !isodd(i[1]+i[2])] # last question

```
