1

I have a list of actions described with strings, ex "BPxPyPzPC" where each letter represents an action and C represents an event.

Some users' actions (such as "B", "Px", "Py" and "Pz") led to an event (in my example, the letter "C"), others did not, so I want to identify the pattern of actions (ex "BPxPyPz") that most often leads to the event, what is the most efficient way to do this in Python?

Thanks!

Example code:

c=['' for x in range(0,4)]
c[0]="BPxPxPyPC"
c[1]="BPxPyPyPC"
c[2]="BPyPxPyPC"
c[3]="BPyPxPyPC"

#do something

#desired result
The most likely sequence of actions to achieve "C" is "BPyPxPy"

2 Answers 2

3

It is not clear if and how do you want to discriminate the actions.

I used regular expressions to match any string followed by C, and Counter to get the most common string.

Here's the simplest thing to obtain your result:

import re
from collections import Counter

c = ["BPxPxPyPC", "BPxPyPyPC", "BPyPxPyPC", "BPyPxPyPC"]

cnt = Counter()
for sequence in c:
    m = re.match('^(.*)C$', sequence)
    if m: cnt.update([m.group(1)])

print('The most likely sequence is " {}"'.format(cnt.most_common(1)[0][0]))
# BPyPxPyP
Sign up to request clarification or add additional context in comments.

Comments

0

I would do something like this (takes into account only first sequence of actions that has max hits in stack for event ev):

def checkSeq(c, stack):
    stackSeqs = [x[0] for x in stack]

    if c not in stackSeqs:
        stack.append([c,0])
    else:
        idx = stackSeqs.index(c)
        stack[idx][1] += 1
    return stack

def max_act_ev(ev, stack):
    acts=[]
    for row in stack:
        if ev in row[0][-1]:
            acts.append(row)
    if len(acts) > 0:
        res = sorted(acts, key=lambda x: x[1],reverse=True)
        return res[0]
    else:
        return []


# Start of code

stack=[["BPyPxPyPC",1],["BPxPxPyPC",1],["BPxPxPxPC",1]]
c = "BPxPxPyPC"
ev = "C"

stack = checkSeq(c,stack)
seq = max_act_ev(ev,stack)

print(stack)
if len(seq)>0:
    print('The most likely sequence of actions to achieve "'+seq[0][-1]+'" is "'+seq[0][:-1]+'"')
else:
    print("No actions found for event "+ev)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.