Created
May 17, 2020 09:43
-
-
Save gijzelaerr/185717c0df43dd0ead0d9da57e53634e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
m collections import defaultdict | |
up = {} | |
for r in messages: | |
if 'parent' in r.keys(): | |
up[r['id']] = r['parent'] | |
else: | |
up[r['id']] = None | |
ancestors = set() | |
for k, v in up.items(): | |
if not v: | |
ancestors.add(k) | |
elif v not in up.keys(): | |
ancestors.add(v) | |
print(ancestors) | |
def find_ancestor(ancestors, id): | |
parent = ancestors.get(id, id) | |
if parent and parent != id: | |
return find_ancestor(ancestors, parent) | |
else: | |
return id | |
threads = defaultdict(list) | |
for message in messages: | |
id = message['id'] | |
ancestor = find_ancestor(up, id) | |
if ancestor != id: | |
threads[ancestor].append(id) | |
print(threads) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment