asq icon indicating copy to clipboard operation
asq copied to clipboard

Frequency distribution table for asq Queryable

Open s-celles opened this issue 5 years ago • 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

s-celles avatar Nov 24 '20 05:11 s-celles

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.

rob-smallshire avatar Nov 24 '20 08:11 rob-smallshire

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!

s-celles avatar Nov 25 '20 16:11 s-celles