Function Repository Resource:

ItemCounts

Source Notebook

A two-argument form of Counts that gives an association between a set of provided keys and the number of times those keys appear in a list

Contributed by: Seth J. Chandler

ResourceFunction["ItemCounts"][list,keys]

gives an Association that pairs each element of keys with the number of times it appears in list.

ResourceFunction["ItemCounts"][list,keys,default]

outputs default for any key not appearing in list.

ResourceFunction["ItemCounts"][list,assoc]

outputs the associated value of a given key in assoc if that key is not an element of list.

ResourceFunction["ItemCounts"][assoc]

is an operator form of ResourceFunction["ItemCounts"] that produces ResourceFunction["ItemCounts"][list,assoc,0] when it is given a list.

Details and Options

The keys of the output Association have the same order as the second argument or, if the second argument is an association, the keys of the second argument.
The default in the third argument of ResourceFunction["ItemCounts"] is usually a single value (like 0), but it can also be a list whose length is the same as that of keys. If the kth key in keys is not present in list, the value in the output associated with the kth key is equal to the kth element of default. If the third argument is not the same length as the second argument, the function takes no action.
Duplicate keys in the second argument are eliminated prior to evaluation of ResourceFunction["ItemCounts"].
Any key not present in the list will appear in the result with a value of 0.

Examples

Basic Examples (4) 

Because neither a "b" nor "e" is present in the first argument, the output Association has values of 0 for those keys; otherwise, the values are just as they would be with Counts:

In[1]:=
ResourceFunction[
 "ItemCounts"][{"d", "d", "c", "d", "d", "a", "c", "c", "d"}, CharacterRange["a", "e"]]
Out[1]=

Keys not found are assumed to take on the default value of 42:

In[2]:=
ResourceFunction[
 "ItemCounts"][{"d", "d", "c", "d", "d", "a", "c", "c", "d"}, CharacterRange["a", "e"], 42]
Out[2]=

The association in the second argument results in missing keys being set to 0, except for key "b", which is set to 42:

In[3]:=
ResourceFunction[
 "ItemCounts"][{"d", "d", "c", "d", "d", "a", "c", "c", "d"}, <|"a" -> 0, "b" -> 42, "c" -> 0, "d" -> 0, "e" -> 0|>]
Out[3]=

An operator form of ItemCounts:

In[4]:=
ResourceFunction["ItemCounts"][CharacterRange["a", "g"]][{"d", "d", "c", "d", "d", "a", "c", "c", "d"}]
Out[4]=

Scope (2) 

Keys in the output have the same order as keys in the second argument of the input:

In[5]:=
ResourceFunction[
 "ItemCounts"][{"d", "d", "c", "d", "d", "a", "c", "c", "d"}, <|"e" -> 0, "a" -> 0, "b" -> 42|>]
Out[5]=

Users can specify their own defaults for a List of values:

In[6]:=
ResourceFunction[
 "ItemCounts"][{"d", "d", "c", "d", "d", "a", "c", "c", "d"}, {"a", "d", "b", "x"}, {3, 1, 4, 0}]
Out[6]=

If one uses a List default, it must be the same length as the List of the keys. Otherwise, an error message is generated:

In[7]:=
ResourceFunction[
 "ItemCounts"][{"d", "d", "c", "d", "d", "a", "c", "c", "d"}, {"a", "b"}, {3, 1, 4, 0}]
Out[7]=

Applications (1) 

A very common use of this function is producing an Association with the same length and same keys when using counting items in a structure of lists:

In[8]:=
Map[ResourceFunction[
  "ItemCounts"][{"a", "b"}], {{"a", "b", "b", "a"}, {"b", "b"}, {"a"}}]
Out[8]=

Properties and Relations (2) 

Another way of going about this is to join a default Association with the Counts, but this method is less flexible:

In[9]:=
Join[<|"a" -> 0, "b" -> 42, "c" -> 0, "d" -> 0, "e" -> 0|>, Counts[{"d", "d", "c", "d", "d", "a", "c", "c", "d"}]]
Out[9]=

One might also do it with a Merge and use the First argument for merging, but this method is similarly less flexible:

In[10]:=
Merge[{Counts[{"d", "d", "c", "d", "d", "a", "c", "c", "d"}], <|"a" ->
     0, "b" -> 42, "c" -> 0, "d" -> 0, "e" -> 0|>}, First]
Out[10]=

Possible Issues (3) 

Duplicate keys in the second argument are eliminated:

In[11]:=
ResourceFunction[
 "ItemCounts"][{"d", "d", "c", "d", "d", "a", "c", "c", "d"}, {"a", "c", "a"}]
Out[11]=

An empty list in the second argument or an empty Association produces an empty Association:

In[12]:=
{ResourceFunction[
  "ItemCounts"][{"d", "d", "c", "d", "d", "a", "c", "c", "d"}, {}], ResourceFunction[
  "ItemCounts"][{"d", "d", "c", "d", "d", "a", "c", "c", "d"}, Association[]]}
Out[12]=

The second argument must be a List or Association:

In[13]:=
ResourceFunction[
 "ItemCounts"][{"d", "d", "c", "d", "d", "a", "c", "c", "d"}, a[2, 3]]
Out[13]=
In[14]:=
{<||>, ResourceFunction[
  "ItemCounts"][{"d", "d", "c", "d", "d", "a", "c", "c", "d"}, a[2, 3]]}
Out[14]=

Neat Examples (2) 

Group the passengers on the Titanic over age 70 by age and count their genders:

In[15]:=
Query[Select[#age > 70 &]/*GroupBy[#age &]/*KeySort, ResourceFunction["ItemCounts"][{"male", "female"}], #sex &][
 ExampleData[{"Dataset", "Titanic"}]]
Out[15]=

Find the distribution of UFO shapes by states, but limit oneself to the five most frequently seen shapes:

In[16]:=
Module[{ufos = ResourceData["UFO Sightings 2015"], popularShapes},
 popularShapes = Normal@Query[
     DeleteMissing[#, 1] &/*Counts/*ReverseSort/*
      Keys/*(Take[#, 5] &), #Shape &][ufos];
 Query[GroupBy[#State &]/*KeySort, ResourceFunction["ItemCounts"][popularShapes], #Shape &][ufos]
 ]
Out[16]=

Publisher

Seth J. Chandler

Version History

  • 1.0.0 – 26 June 2019

Related Resources

License Information