import itertools
alphabet = ["a", "b", "c"]
other_alphabet = ["a", "b", "d"]
another_alphabet = ["a", "b", "c"]
# here we take the nth char of each alphabet that is used as prediction
# (this position is indicated by the number of the currently entered char)
# zip takes the lists, and well, zips :) them, it means it creates new lists, so every
# first elements end up together, second elemends are together and so on.
# as far as current position you have to track it when user enters data (you have to
# know which (first, second, tenth) letter user is entering
current_position=1
letters = zip(alphabet,other_alphabet, another_alphabet)[current_position]
letters = list(letters)
letters.sort()
print 'letters at current position', letters
# here we group all occurences of the same letters,
letter_groups = itertools.groupby(letters, key=lambda x: x[0])
# here we count the number of occurences of each letter
# from the alphabets, and divide it by the lenght
# of the list of letters
letter_probabilities = [[a[0], sum (1 for _ in a[1])/float(len(letters))] for a in letter_groups]
print 'letter probablilities at the current postion ', letter_probabilities
上記のコードは、次の出力を生成します。
letters at current position ['c', 'c', 'd']
letter probablilities at the current postion [['c', 0.6666666666666666], ['d', 0.3333333333333333]]