Skip to content

Instantly share code, notes, and snippets.

@zaphar
Forked from artdent/graph.clj
Created January 16, 2012 21:44
Show Gist options
  • Save zaphar/1623184 to your computer and use it in GitHub Desktop.
Save zaphar/1623184 to your computer and use it in GitHub Desktop.
Ways by node
(defn collect-ways-by-node [ways]
(reduce (fn [m m2] (merge-with #(concat %1 %2) m1 m2))
(for [way ways]
(for [id (:nodes way)]
(hash-map (:id way) [id])))))
import collections
def ways_by_node(ways):
ret = collections.defaultdict(list)
for way in ways:
way_id = way['id']
for node_id in way['nodes']:
ret[node_id].append(way_id)
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment