Skip to main content
 首页 » 编程设计

python之如何跳过python列表中的当前元素

2025年05月04日45over140

我如何遍历 python 列表,将每个项目添加到除当前项目之外的另一个列表:

list = [1,2,8,20,11] 
 
for i,j in enumerate(list): 
    print j[i+1:] 
#this will only give [2,8,20,11] then [8,20,11],[20,11], [11] 
 
#but I want something like [2,8,20,11], [1,8,20,11],[1,2,20,11]... etc. 
#then 1,8,20,11 
#then  

请您参考如下方法:

看起来你在寻找组合,例如:

>>> from itertools import combinations 
>>> data = [1,2,8,20,11] 
>>> list(combinations(data, 4)) 
[(1, 2, 8, 20), (1, 2, 8, 11), (1, 2, 20, 11), (1, 8, 20, 11), (2, 8, 20, 11)]