Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frequency distribution table for asq Queryable #23

Closed
s-celles opened this issue Nov 24, 2020 · 2 comments
Closed

Frequency distribution table for asq Queryable #23

s-celles opened this issue Nov 24, 2020 · 2 comments

Comments

@s-celles
Copy link

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

@rob-smallshire
Copy link
Collaborator

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.

@s-celles
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants