Quick implementation of n-grams of characters using python

微信扫一扫,分享到朋友圈

Quick implementation of n-grams of characters using python

I wrote the following code for computing character bigrams and the output is right below. My question is, how do I get an output that excludes the last character (ie t)? and is there a quicker and more efficient method for computing character n-grams?

b='student'
>>> y=[]
>>> for x in range(len(b)):
    n=b[x:x+2]
    y.append(n)
>>> y
['st', 'tu', 'ud', 'de', 'en', 'nt', 't']

Here is the result I would like to get: ['st','tu','ud','de','nt]

Thanks in advance for your suggestions.

To generate bigrams:

In [8]: b='student'

In [9]: [b[i:i+2] for i in range(len(b)-1)]
Out[9]: ['st', 'tu', 'ud', 'de', 'en', 'nt']

To generalize to a different n
:

In [10]: n=4

In [11]: [b[i:i+n] for i in range(len(b)-n+1)]
Out[11]: ['stud', 'tude', 'uden', 'dent']

微信扫一扫,分享到朋友圈

Quick implementation of n-grams of characters using python

I switched my $1000 iPhone X for a $200 phone and it was not all that surprising

上一篇

Megasolid Idiom

下一篇

你也可能喜欢

Quick implementation of n-grams of characters using python

长按储存图像,分享给朋友