I have these game logs which I need to clean, process and find the frequency of game actions. These game actions should be a triplet. In the below given list,
"1|123|/jump", "2|123|/flip", "3|123|/crouch"
is a triplet and also "2|123|/flip", "3|123|/crouch", "8|123|/dance"
.
Similarly, "4|999|/jump", "5|999|/flip", "6|999|/crouch"
, "5|999|/flip", "6|999|/crouch", "7|999|/stroll"
, and "6|999|/crouch", "7|999|/stroll", "9|999|/jump"
.
And finally, "10|639|/flip", "11|639|/crouch","12|639|/stroll"
.
The output should be a dictionary of action triplets with keys as action triplets and value is the frequency of the action triplets.
{'/jump,/flip,/crouch': 2, '/flip,/crouch,/dance': 1, '/flip,/crouch,/stroll': 2, '/crouch,/stroll,/jump': 1}
and the user_id which has the highest number of action triplets. In this case it is user_id: 999
The following is my approach:
- I split the string on
|
and create a list of tuples(user_id, action)
. I ignore the sequence id (first part of the log). - I sort the list of tuples by
user_id
. - I create triplets by comparing the three consecutive
user_ids
and create a 2-d list. - Create a dictionary with key (append each
user_action
together with a comma) from each list of action triplets and value being the count of these same set of user_actions. - Create another dictionary with key
(user_id)
- I took the very first value of the tuple from each list as each triplet list can only have oneuser_id
. - Then, I return the max value key i.e.,
user_id
at the end.
game_logs = ["1|123|/jump", "2|123|/flip", "3|123|/crouch", "4|999|/jump", "5|999|/flip", "6|999|/crouch", "7|999|/stroll", "8|123|/dance", "9|999|/jump", "10|639|/flip", "11|639|/crouch","12|639|/stroll"]
def clean(game_logs):
size = len(game_logs)
user_id_actions = []
for game_log in game_logs:
id, user_id, action = extract(game_log)
user_id_actions.append((user_id, action))
user_id_actions.sort(key = lambda x:x[0])
# print(user_id_actions)
action_triplets = []
for i in range(size - 2):
if user_id_actions[i][0] == user_id_actions[i+1][0] and user_id_actions[i+1][0] == user_id_actions[i+2][0]:
action_triplets.append([user_id_actions[i], user_id_actions[i+1], user_id_actions[i+2]])
# print(action_triplets)
triplet_hash = {}
max_actions_user_id_hash = {}
for triplet in action_triplets:
page_string = []
for val in triplet:
user_id, page = val
page_string.append(page)
fin_string = ','.join(page_string)
if fin_string not in triplet_hash:
triplet_hash[fin_string] = 1
else:
triplet_hash[fin_string] += 1
# print(triplet_hash)
print(f"Action action_triplets: {triplet_hash}")
for triplet in action_triplets:
user_id = triplet[0][0]
if user_id not in max_actions_user_id_hash:
max_actions_user_id_hash[user_id] = 1
else:
max_actions_user_id_hash[user_id] += 1
# print(max_actions_user_id_hash)
print(f"Highest number of action_triplets are for the user_id :{max(max_actions_user_id_hash, key=max_actions_user_id_hash.get)}")
def extract(game_log):
extract_info = tuple(game_log.split('|'))
return extract_info
clean(game_logs)
It feels like I over complicated the whole logic here. Creating nested lists, creating multiple dictionaries. Could anyone share feedback.
Thank you
It feels like I over complicated the whole logic here.
You seem to have proceeded from what have I got. Change perspective: Work backwards from what do I need. For a decent review, add mode context. \$\endgroup\$