Skip to main content
 首页 » 编程设计

python之在 Python 中嵌套 for

2025年05月04日10bhlsheji

我有一个列表,我想对其进行迭代,然后在嵌套的 for 中迭代,将第一个列表从下一个位置开始迭代我读过的第一个。

在像 Java 这样的语言中:

int[10] array; 
for (int i=0; i < array.length(); i++) 
    for (int j=i+1; j < array.length(); j ++) 
        //do something with the array comparing a[i] and a[j] 

我如何在 Python 上做到这一点? 我试试这个:

for a in array: 
     del array[0] 
     for a2 in array: 
         //do something with the array comparing a and a2 

但只在第一次迭代中有效..有什么帮助吗?

请您参考如下方法:

for i in range(0,len(array)): 
    for j in range(i+1,len(array)): 
        #do something with array[i] and array[j]