How to iterate on bhramam?

There are bigrams from the text obtained with the following code:
token = nltk.word_tokenize(train_words)
bigrams = ngrams(token, 2)
print(list(bigrams))

Output (reduced):
[('This', 'house'), ('house', 'That'), ('Which', 'built'), ('built', 'Jack'), ('Jack', 'A'), ('And', 'it'), ('is', 'wheat'), ('wheat', 'Which'), ('That', 'in'), ('in', 'dark'), ('dark', 'the closet'), ('closet', 'is'), ('is', 'In'), ('In', 'house'), ('house', 'That'), ('Which', 'built'), ('built', 'Jack'), ('Jack', 'A'), ('And', 'it'), ('is', 'fun'), ('fun', 'bird-bird'), ('bird-bird', 'That'), ('Which', 'often'), ('often', 'stealing'), ('stealing', 'wheat'), ('wheat', 'Which'), ('That', 'in'), ..., ]

Question: is it necessary, for example, is banal to protonirovanie -- first output the first bigram in the list, and then the first word of the first bigram of a list. How to do it?
Try simple:
for bigram in bigrams:
 print(bigram)

But jupyter does not display anything at all. What's the matter?
June 14th 19 at 19:07
2 answers
June 14th 19 at 19:09
Solution
I don't know why, but the output print(list(bigrams)) breaks the implementation for, if you thecommunity, the normal cycle will work, but you might want something like this:
lst= list(bigrams)
print(lst)
bigrams=iter(lst)
I think because ngrams is a function which returns a generator, and the generator you can go only once and makes a call to list(), therefore, the further loop does nothing

>>> def ngrams():
... for i in [1,2,3]:
... yield i
... 
>>> s = ngrams()
>>> list(s)
[1, 2, 3]
>>> for i in s:
... print(i)
... 
>>> s = ngrams()
>>> for i in s:
... print(i)
... 
1
2
3
>>> list(s)
[]

upd: Yes, if you look in the source then that's what she does. - alexandro_Auer commented on June 14th 19 at 19:12
June 14th 19 at 19:11
Solution
Above already answered - because ngrams returns a generator for walking in the cycle only once. I can do a N independent iterators and experiment)))
from itertools import tee
bgen1, bgen2, bgen3 = tee(ngrams(token, 2), 3)

Find more questions by tags Natural language processingText processingPython