sixty-north/asq

Frequency distribution table for asq Queryable

Closed this issue · 2 comments

Hello,

this is probably a bit out of the scope of asq but maybe implementing a frequency distribution table method could be a nice idea (either adding it directly in asq code, or as an example to extend asq).
The result of this frequency distribution table will be queryable also
Your opinion ?

Kind regards

The API is deliberately fairly close to the original LINQ API. I'd be happy to include something like that as an example. In fact, I have exactly that example around here somewhere.

Here is a quite naive (and probably not very efficient) implementation

identity = lambda x: x


def freqtable(iterable, func=identity, descending=True):
    ft = {}
    for elt in iterable:
        value = func(elt)
        if func(elt) not in ft:
            ft[value] = 1
        else:
            ft[value] += 1
    ft = [(k, v) for k, v in ft.items()]
    ft = sorted(ft, key=lambda e: e[1], reverse=descending)
    return ft


iterable = [
    {"v": "a"},
    {"v": "b"},
    {"v": "c"},
    {"v": "d"},
    {"v": "e"},
    {"v": "f"},
    {"v": "e"},
    {"v": "b"},
    {"v": "b"},
]
print(iterable)
func = lambda elt: elt["v"]
ft = freqtable(iterable, func=func)
print(ft)

integrating with asq and making use of iterators will be much better!