Assignment on 2 gram

This is with respect to the most common 2 gram problem given as assignment.

Are the 2 gram say, (age after) and ( after age) considered as different. Because when Sir was explaining he said that 2 grams words help to identify which 2 words occur most frequently. So the order also matters or not

Yes, as this problem is given on a simpler level, they both will be considered as different words.

Thanks for the clarification. I have attempted to consider both as one and the same using the below code. There might be possibly a better way as this is my first learning of Python.

two_gram_dict= {}
for i in range(0, len(A_tuple)):
    temp=0
    A_value=A_tuple[i]
    reverse_A=(A_tuple[i][1],A_tuple[i][0])
    
    if (A_value in two_gram_dict.keys()):
        temp=two_gram_dict.get(A_value)
        temp = temp+1
        two_gram_dict.update({A_value:temp})
    elif (reverse_A in two_gram_dict.keys()):
        temp=two_gram_dict.get(reverse_A)
        
        temp = temp+1
        two_gram_dict.update({reverse_A:temp})
    else:
        two_gram_dict.update({A_value:1})
    
    del reverse_A
print (two_gram_dict)
1 Like