Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gijzelaerr/185717c0df43dd0ead0d9da57e53634e to your computer and use it in GitHub Desktop.
Save gijzelaerr/185717c0df43dd0ead0d9da57e53634e to your computer and use it in GitHub Desktop.
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