# Counting words in StringArray

**URL:** <https://discourse.julialang.org/t/counting-words-in-stringarray/47398>\
**Category:** General Usage\
**Created:** [September 28, 2020, 10:29am UTC](https://discourse.julialang.org/t/counting-words-in-stringarray/47398 "2020-09-28T10:29:48Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [September 28, 2020, 10:29am UTC](https://discourse.julialang.org/t/counting-words-in-stringarray/47398/1 "2020-09-28T10:29:48Z")

</div>

How to tount words in StringArray

```julia
julia> y=["gastro" "gastro gastro "; "dom" "pies"]
2×2 Array{String,2}:
 "gastro" "gastro gastro "
 "dom" "pies"

julia> occursin.(x,y)
2×2 BitArray{2}:
 1 1
 0 0

julia> y.=="gastro"
2×2 BitArray{2}:
 1 0
 0 0

```

I am looking for a solution that will give:

```julia
2×2 BitArray{2}:
 1 2
 0 0

```

Paul

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 28, 2020, 10:37am UTC](https://discourse.julialang.org/t/counting-words-in-stringarray/47398/2 "2020-09-28T10:37:28Z")

</div>

Can you do it if you just want to count the words in a string, for example how many `"gastro"` in `"gastro gastro"`? When you have that working you can use a broadcasting with that function over the array.

---

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [September 28, 2020, 10:47am UTC](https://discourse.julialang.org/t/counting-words-in-stringarray/47398/3 "2020-09-28T10:47:03Z")

</div>

> [@programista](#):
>
> `occursin.(x,y)`

I am reading:

```julia
julia> broadcast(+, A, B)

```

but

```julia
julia> broadcast.(occursin.(x,y))
ERROR: MethodError: objects of type Bool are not callable
Stacktrace:

```

---

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [September 28, 2020, 10:49am UTC](https://discourse.julialang.org/t/counting-words-in-stringarray/47398/4 "2020-09-28T10:49:45Z")

</div>

I was found like this

```julia
julia> using DataStructures
julia> count.(x,y)
2×2 Array{Int64,2}:
 1 2
 0 0

julia> x
"gastro"

julia> y
2×2 Array{String,2}:
 "gastro" "gastro gastro "
 "dom" "pies"

julia> using DataStructures

julia> count.(x,y)
2×2 Array{Int64,2}:
 1 2
 0 0

```

Is it OK? , fast ?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 28, 2020, 11:26am UTC](https://discourse.julialang.org/t/counting-words-in-stringarray/47398/5 "2020-09-28T11:26:49Z")

</div>

That works, but you don’t need `DataStructures` for this, as far as I can tell.

---

<div class="post-metadata">

**Author:** ![Sanjan\_Das](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sanjan_das/32/17886_2.png) [@Sanjan\_Das](https://discourse.julialang.org/u/Sanjan_Das)\
**Post date:** [September 28, 2020, 2:21pm UTC](https://discourse.julialang.org/t/counting-words-in-stringarray/47398/6 "2020-09-28T14:21:43Z")

</div>

I think this should be as fast as it can get for this case as it’s now a vectorised operation

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 28, 2020, 5:15pm UTC](https://discourse.julialang.org/t/counting-words-in-stringarray/47398/7 "2020-09-28T17:15:36Z")

</div>

Vectorized isn’t faster, a loop would be just as good. But the chosen solution is probably quite fast, and also elegant.

---

<div class="post-metadata">

**Author:** ![Sanjan\_Das](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sanjan_das/32/17886_2.png) [@Sanjan\_Das](https://discourse.julialang.org/u/Sanjan_Das)\
**Post date:** [September 28, 2020, 6:30pm UTC](https://discourse.julialang.org/t/counting-words-in-stringarray/47398/8 "2020-09-28T18:30:14Z")

</div>

Oh really? Is it not like Python in this regard, where vectorisation is faster than loops because of more efficient memory handling?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [September 28, 2020, 6:33pm UTC](https://discourse.julialang.org/t/counting-words-in-stringarray/47398/9 "2020-09-28T18:33:17Z")

</div>

What do you call “more efficient memory handling”?

I think that in Julia, fusing the dots, this is, calling broadcasted function over the result of broadcasted function may eliminate intermediary data structures, however, a loop may do the same, depending on how it is written.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 28, 2020, 7:00pm UTC](https://discourse.julialang.org/t/counting-words-in-stringarray/47398/10 "2020-09-28T19:00:53Z")

</div>

Nope, it’s not like Python in that regard. The reason vectorization is fast in Python isn’t memory handling, but that the calculation is handed off to a library written in a fast language like C, that just does looping.

In Julia, Julia itself is that fast language (except for some special cases like BLAS, which isn’t yet implemented in Julia because of the workload that would be).

The thing you think of as ‘fast vectorization’ is in most cases just looping under the hood in a faster language.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 28, 2020, 7:08pm UTC](https://discourse.julialang.org/t/counting-words-in-stringarray/47398/11 "2020-09-28T19:08:53Z")

</div>

In fact, memory handling for vectorized code in ‘slow’ languages like Python and Matlab is normally quite bad, because you chain together vectorized operations that each produce intermediate temporary arrays. This can be avoided in Julia, either with “fused broadcasting” or with plain loops.

---

<div class="post-metadata">

**Author:** ![Sanjan\_Das](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sanjan_das/32/17886_2.png) [@Sanjan\_Das](https://discourse.julialang.org/u/Sanjan_Das)\
**Post date:** [September 28, 2020, 11:02pm UTC](https://discourse.julialang.org/t/counting-words-in-stringarray/47398/12 "2020-09-28T23:02:18Z")

</div>

Thanks for clarifying that! I’m a newbie to Julia, and this good information to know.

---

<div class="post-metadata">

**Author:** ![Sanjan\_Das](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sanjan_das/32/17886_2.png) [@Sanjan\_Das](https://discourse.julialang.org/u/Sanjan_Das)\
**Post date:** [September 29, 2020, 8:10am UTC](https://discourse.julialang.org/t/counting-words-in-stringarray/47398/13 "2020-09-29T08:10:20Z")

</div>

For anyone interested in this further, there’s an article explaining it from the time it was introduced in Julia 0.6

> **[More Dots: Syntactic Loop Fusion in Julia](https://julialang.org/blog/2017/01/moredots/)**
>
> More Dots: Syntactic Loop Fusion in Julia | After a lengthy design process (https://github.com/JuliaLang/julia/issues/8450) and preliminary foundations in Julia 0.5 (/blog/2016-10-11-julia-0.5-highlights#vectorized\_function\_calls), Julia 0.6 includes...
