Forming Bigrams of words in list of sentences with Python

前端 未结 10 1507
遇见更好的自我
遇见更好的自我 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 02:46

    Using list comprehensions and zip:

    >>> text = ["this is a sentence", "so is this one"]
    >>> bigrams = [b for l in text for b in zip(l.split(" ")[:-1], l.split(" ")[1:])]
    >>> print(bigrams)
    [('this', 'is'), ('is', 'a'), ('a', 'sentence'), ('so', 'is'), ('is', 'this'), ('this',     
    'one')]
    

提交回复
热议问题