小编Mic*_*mos的帖子

从列表中选择相邻的夫妻(或三元组等)

我可以通过使用列表索引来做到这一点......

lst =[1,2,3,4,5,6]
[ [lst[i] , lst[i+1]] for i in range( len(lst) - 1 )]
Run Code Online (Sandbox Code Playgroud)

要么:

lst =[1,2,3,4,5,6]
for i in range(len(lst)-1): 
    entities.append([lst[i],lst[i+1]])
Run Code Online (Sandbox Code Playgroud)

但是有更聪明的方法吗?也许使用迭代器?性能怎么样?

python python-2.7 python-3.x

4
推荐指数
1
解决办法
123
查看次数

标签 统计

python ×1

python-2.7 ×1

python-3.x ×1