0

I have list as follows

string = ['I went to work but got delayed at other work and got stuck in a traffic']

Now I want a output like as follows

stirng_seq = [(I, went), (went,to), (to,work), (work,but).....(in,a),(a,traffic)]

To clarify, I want a list of tuples where words are sequential.

My approach so far,

words = list(set(word.lower() for t in string for word in t.split()))
inv_txt = [(i,j) for i,j in zip(words[:-1],words[1:])]

However this is producing all combination of word pairs which I do not want. Like

[('went', 'got'), ('got', 'at'), ('at', 'and'), ('and', 'stuck'), ('stuck', 'traffic'), ('traffic', 
'but'), ('but', 'in'), ('in', 'other'), ('other', 'work'), ('work', 'to'), ('to', 'i'), ('i', 
 'delayed'), ('delayed', 'a')] 

Any clue on this? Even itertools.permutations() seems not to have worked.

pythondumb
  • 1,187
  • 1
  • 15
  • 30

1 Answers1

2
your_string = 'I went to work but got delayed at other work and got stuck in a traffic'
your_list = your_string.split()
your_result = list(zip(your_list, your_list[1:]))

Your mistake was at using set where you lost the order.

ferhatelmas
  • 3,818
  • 1
  • 21
  • 25