Forming Bigrams of words in list of sentences with Python

前端 未结 10 1492
遇见更好的自我
遇见更好的自我 2020-12-24 02:16

I have a list of sentences:

text = [\'cant railway station\',\'citadel hotel\',\' police stn\']. 

I need to form bigram pairs and store the

10条回答
  •  抹茶落季
    2020-12-24 03:01

    >>> text = ['cant railway station','citadel hotel',' police stn']
    >>> bigrams = [(ele, tex.split()[i+1]) for tex in text  for i,ele in enumerate(tex.split()) if i < len(tex.split())-1]
    >>> bigrams
    [('cant', 'railway'), ('railway', 'station'), ('citadel', 'hotel'), ('police', 'stn')]
    

    Using enumerate and split function.

提交回复
热议问题