Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- now we have data with 3 properties
- user_id, timestamp, action
- 100, 1000, A. 留学申请论坛-⼀亩三分地
- 200, 1003, A
- 300, 1009, B
- 100, 1026, B
- 100, 1030, C. 1point3acres
- 200, 1109, B
- 200, 1503, A
- We want to output a graph to visualize it
- Graph from input:
- |---A (2)
- | |---B (2)
- | | |---C (1)
- | | |---A (1)
- |---B (1)
- aggregate by user_id:
- 100 : A->B->C
- 200: A->B->A
- 300: B
- """
- def build_log_system(logs):
- user_id, time_stamp, action = zip(*logs)
- user_id = sorted(list(set(user_id)))
- # construct a trie
- root = {'#': '#'}
- for uid in user_id:
- t = root
- for log in logs:
- if log[0] == uid:
- if log[2] not in t:
- t[log[2]] = {'counter': 1}
- else:
- t[log[2]]['counter'] += 1
- t = t[log[2]]
- t['#'] = '#'
- # traversal
- def dfs(node, level_so_far=''):
- if node == '#':
- return
- for next_action in node:
- if next_action not in ['counter', '#']:
- print('{}|---{}({})'.format(level_so_far, next_action, node[next_action]['counter']))
- dfs(node[next_action], level_so_far + '|\t')
- dfs(root)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement