Skip to content

Instantly share code, notes, and snippets.

@gidgid
Last active January 30, 2021 12:52
Show Gist options
  • Save gidgid/6a36d3fd10d7cac0a91a94f4fbe6df63 to your computer and use it in GitHub Desktop.
Save gidgid/6a36d3fd10d7cac0a91a94f4fbe6df63 to your computer and use it in GitHub Desktop.
shows how we can manually run flatten
from typing import Dict, Set
def flatten_multivalues(key_to_values: Dict[str, Set[int]]) -> Set[int]:
all_values = set()
for values in key_to_values.values():
all_values.update(values)
return all_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