如何从此列表中创建 bi_grams?

问题描述

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}

解决方法

如何从此列表创建 bi_grams ?

text = ['to','be',','or','not','to','that','is','the','question',':']

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...