Forming Bigrams of words in list of sentences with Python

前端 未结 10 1494
遇见更好的自我
遇见更好的自我 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条回答
  •  猫巷女王i
    2020-12-24 02:50

    There are a number of ways to solve it but I solved in this way:

    >>text = ['cant railway station','citadel hotel',' police stn']
    >>text2 = [[word for word in line.split()] for line in text]
    >>text2
    [['cant', 'railway', 'station'], ['citadel', 'hotel'], ['police', 'stn']]
    >>output = []
    >>for i in range(len(text2)):
        output = output+list(bigrams(text2[i]))
    >>#Here you can use list comphrension also
    >>output
    [('cant', 'railway'), ('railway', 'station'), ('citadel', 'hotel'), ('police', 'stn')]
    

提交回复
热议问题