Äquivalent zu Pythons „collections.Counter“ in DolphinDBPython

Python-Programme
Anonymous
 Äquivalent zu Pythons „collections.Counter“ in DolphinDB

Post by Anonymous »

Ich möchte die Vorkommen jedes Elements in einem DolphinDB-Array zählen, ähnlich wie „collections.Counter“ in Python funktioniert.

Code: Select all

from collections import Counter
my_list = [1, 2, 3, 2, 1, 3, 2, 1]
my_counter = Counter(my_list)
print(my_counter)  # Output: Counter({1: 3, 2: 3, 3: 2})
In DolphinDB habe ich versucht, die Groupby-Funktion zu verwenden, aber bei Verwendung der Count-Funktion wird ein Fehler ausgegeben:

Code: Select all

myList = [1, 2, 3, 2, 1, 3, 2, 1]
groupby(count, myList, myList)
Der Fehler ist:

Verwendung: groupby(func, funcArgs, groupingCol). func muss eine Funktionsdefinition sein.

Interessanterweise funktioniert es ohne Syntaxfehler, wenn ich count durch avg ersetze (obwohl es mir nicht die Häufigkeit gibt, die ich brauche):

Code: Select all

groupby(avg, myList, myList) // This works and returns (1->1, 2->2, 3->3, but it is not desired output:  {1: 3, 2: 3, 3: 2} )
Warum schlägt Groupby mit count fehl, funktioniert aber mit avg? Was ist die idiomatische Art, Elementhäufigkeiten in DolphinDB zu zählen?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post