使用Python按顺序排列的一对单词的列表

使用Python按顺序排列的一对单词的列表,python,Python,我的清单如下 string = ['I went to work but got delayed at other work and got stuck in a traffic'] stirng_seq = [(I, went), (went,to), (to,work), (work,but).....(in,a),(a,traffic)] 现在我想要一个如下的输出 string = ['I went to work but got delayed at other work and g

我的清单如下

string = ['I went to work but got delayed at other work and got stuck in a traffic']
stirng_seq = [(I, went), (went,to), (to,work), (work,but).....(in,a),(a,traffic)]
现在我想要一个如下的输出

string = ['I went to work but got delayed at other work and got stuck in a traffic']
stirng_seq = [(I, went), (went,to), (to,work), (work,but).....(in,a),(a,traffic)]
为了澄清,我想要一个元组列表,其中单词是连续的

到目前为止,我的方法

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:])]
然而,这产生了我不想要的所有单词对的组合。像

[('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')] 
有线索吗?甚至
itertools.permutations()
似乎也不起作用

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:]))
您的错误是在使用
set
时丢失了订单


您的错误是在使用
set
时丢失了订单。

是的,这也是可行的。是的,这也是可行的。