如何从此列表创建 bi_grams ?
text = ['to','be',',','or','not','to','be',',','that','is','the','question',':']
text = ['to','be',',','or','not','to','be',',','that','is','the','question',':'] bi_grams = [(text[t1], text[t1+1]) for t1 in range(len(text)-1)] bg = [(x, bi_grams.count(x)) for x in list(set(bi_grams))] bg.sort(key=lambda x:x[1], reverse=True) result = {x[0]: x[1] for x in bg} print(result)
输出
{('to', 'be'): 2, ('be', ','): 2, ('that', 'is'): 1, ('question', ':'): 1, ('is', 'the'): 1, ('the', 'question'): 1, (',', 'or'): 1, ('not', 'to'): 1, ('or', 'not'): 1, (',', 'that'): 1}