Count works differently depending on how an association was created
As Kirill Belov notes in a comment, the issue is related to the fact that the list a1
is a packed array (generated by Range
) whereas the list a2
is not packed. Count
, Position
and Depth
unexpectedly act as if the packed array is atomic. This is very likely a bug since 1) the expected behaviour occurs if the top-level expression is a list instead of an association and 2) many other level-sensitive functions yield the expected results.
Analysis (current as of version 11.1)
For discussion purposes, let us consider the following two associations:
packed = <| "a" -> Developer`ToPackedArray[{1, 2}] |>;
unpacked = <| "a" -> Developer`FromPackedArray[{1, 2}] |>;
We will apply various operators to these values:
The results show that Count
, Position
and Depth
act as if the packed array were atomic. The results for these operations can be explained by the TreeForm
structure diagrams shown in the table if we consider all of the internal association structural details to be a "single level" (i.e. the AssociationNodes
from Assocation
down to Rule
).
On the other hand, these results are not consistent with those operations that appear in the table below the structure diagrams. Cases
, Level
, Total
, Map
and Replace
all treat the packed array as if it were not atomic.
Furthermore, even Count
, Position
and Depth
stop treating the packed array as atomic if the top-level expression is a list instead of an association:
We can see from this second table the results are all consistent for the various level-sensitive operators -- except when Count
, Position
and Depth
acting upon a packed array contained within an association. This is almost certainly a bug.