Skip to content

Instantly share code, notes, and snippets.

@gidgid
Created January 30, 2021 12:48
Show Gist options
  • Save gidgid/c24e9efdaf4713cfc98da5213dd7757b to your computer and use it in GitHub Desktop.
Save gidgid/c24e9efdaf4713cfc98da5213dd7757b to your computer and use it in GitHub Desktop.
we use more_itertools flatten to do a much better job
from typing import Dict, Set
from more_itertools import flatten
def flatten_multivalues(key_to_values: Dict[str, Set[int]]) -> Set[int]:
return set(flatten(key_to_values.values()))
def test_flattens_multivalue_dicts():
# shamelessly taken from: https://www.elastic.co/blog/found-elasticsearch-from-the-bottom-up
term_to_document_indices = {
"choice": {3},
"coming": {1},
"fury": {2},
"is": {1, 2, 3},
"the": {2, 3},
}
all_document_indices = flatten_multivalues(term_to_document_indices)
assert all_document_indices == {1, 2, 3}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment