获取列表的连续相邻元素

里奇

我正在寻找从列表中创建连续的单词序列组合的方法。

news = ['Brendan', 'Rodgers', 'has', 'wasted', 'no', 'time', 'in', 'playing', 'mind', '
games', 'with', 'Louis', 'van', 'Gaal', 'by', 'warning', 'the', 'new', 'Manchest
er', 'United', 'manager', 'that', 'the', 'competitive', 'nature', 'of', 'the', '
Premier', 'League', 'will', 'make', 'it', 'extremely', 'difficult', 'for', 'the'
, 'Dutchman', 'to', 'win', 'the', 'title', 'in', 'his', 'first', 'season.']

我猜下面的代码效率不高。有没有一种单线的或更pythonic的方式来实现这一目标?

wordseq = []
for i,j in enumerate(news):
    if len(news)-1 != i:
        wordseq.append((j, news[i+1]))

我想要的结果是这样;

[('Brendan', 'Rodgers'), ('Rodgers', 'has'), ('has', 'wasted'), ('wasted', 'no')
, ('no', 'time'), ('time', 'in'), ('in', 'playing'), ('playing', 'mind'), ('mind
', 'games'), ('games', 'with'), ('with', 'Louis'), ('Louis', 'van'), ('van', 'Ga
al'), ('Gaal', 'by'), ('by', 'warning'), ('warning', 'the'), ('the', 'new'), ('n
ew', 'Manchester'), ('Manchester', 'United'), ('United', 'manager'), ('manager',
 'that'), ('that', 'the'), ('the', 'competitive'), ('competitive', 'nature'), ('
nature', 'of'), ('of', 'the'), ('the', 'Premier'), ('Premier', 'League'), ('Leag
ue', 'will'), ('will', 'make'), ('make', 'it'), ('it', 'extremely'), ('extremely
', 'difficult'), ('difficult', 'for'), ('for', 'the'), ('the', 'Dutchman'), ('Du
tchman', 'to'), ('to', 'win'), ('win', 'the'), ('the', 'title'), ('title', 'in')
, ('in', 'his'), ('his', 'first'), ('first', 'season.'), ('Brendan', 'Rodgers'),
 ('Rodgers', 'has'), ('has', 'wasted'), ('wasted', 'no'), ('no', 'time'), ('time
', 'in'), ('in', 'playing'), ('playing', 'mind'), ('mind', 'games'), ('games', '
with'), ('with', 'Louis'), ('Louis', 'van'), ('van', 'Gaal'), ('Gaal', 'by'), ('
by', 'warning'), ('warning', 'the'), ('the', 'new'), ('new', 'Manchester'), ('Ma
nchester', 'United'), ('United', 'manager'), ('manager', 'that'), ('that', 'the'
), ('the', 'competitive'), ('competitive', 'nature'), ('nature', 'of'), ('of', '
the'), ('the', 'Premier'), ('Premier', 'League'), ('League', 'will'), ('will', '
make'), ('make', 'it'), ('it', 'extremely'), ('extremely', 'difficult'), ('diffi
cult', 'for'), ('for', 'the'), ('the', 'Dutchman'), ('Dutchman', 'to'), ('to', '
win'), ('win', 'the'), ('the', 'title'), ('title', 'in'), ('in', 'his'), ('his',
 'first'), ('first', 'season.')]
黄鼠狼

使用zip

wordseq = zip(news,news[1:])

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从列表生成相邻元素

来自分类Dev

获取列表中所有相邻元素的元组

来自分类Dev

获取在列表中重复n次的连续元素

来自分类Dev

列表中的连续元素

来自分类Dev

列表中的连续元素

来自分类Dev

在列表中查找元素并保留相邻元素

来自分类Dev

对列表中的连续元素对求和

来自分类Dev

Prolog-在列表中查找相邻元素

来自分类Dev

获取元素的连续比率python

来自分类Dev

如何从连接表中连续获取列而不是相邻列

来自分类Dev

获取列表中任意 2 个连续元素之间的最大差异的 Pythonic 方法

来自分类Dev

查找列表中大于其相邻元素的元素数量

来自分类Dev

按连续的公共元素拆分列表

来自分类Dev

查找列表中连续增加的元素数

来自分类Dev

在列表的连续元素之间创建关系

来自分类Dev

查找列表中连续增加的元素数

来自分类Dev

Erlang在列表中查找连续的相同元素

来自分类Dev

如何在列表中查找元素是连续的

来自分类Dev

删除链接列表中的连续元素

来自分类Dev

计算列表中连续元素的数量

来自分类Dev

获取数组的n个连续元素

来自分类Dev

获取数组中连续元素的最大总和

来自分类Dev

获取元素的XPath列表

来自分类Dev

从列表中获取元素

来自分类Dev

获取元素的XPath列表

来自分类Dev

从列表中获取元素

来自分类Dev

获取列表元素的组合对

来自分类Dev

获取元素作为列表

来自分类Dev

获取列表元素组合的列表