1

Python を使用して、ある種の予測/提案を実行する必要があります。

たとえば、複数のリストがあると仮定しましょう

alphabet = ["a", "b", "c"]

other_alphabet = ["a", "b", "d"]

another_alphabet = ["a", "b", "c"]

建設中の現在のアルファベットもあります

current_alphabet = ["a", "b", ...]

3 つのアルファベットのうちの 2 つは"a", "b""c"後のcurrent_alphabet文字"a", "b""c"

タスクは見た目よりも少し複雑だと思います。

これを達成する方法について何か提案はありますか? たぶん、このプロセスに役立つ何か似たようなものはありますか?

4

1 に答える 1

1
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]]
于 2013-10-28T16:12:10.783 に答える