Python for循环启动计数器初始化

时间:2013-07-06 00:24:06

标签: python loops for-loop

for iteration in range(len(list) - 1):
  index = iteration +1 #This is the line which has no effect on the inner loop
  for index in range(len(list)):
    if list[iteration] > list[index]:
      newmin  = list[index]
      newminindex = index        
  if iteration != newminindex :
    swapnumbers(list,iteration, newminindex)

以上是我为选择排序算法编写的代码片段。但是我看到内循环启动计数器总是从0开始。请求专家评论。

5 个答案:

答案 0 :(得分:7)

for index in range(len(list))循环执行循环体,index首先设置为0,然后1,然后2等,直至{{1} }}。 len(list) - 1的先前值被忽略并被覆盖。如果您希望indexindex开始,请使用iteration + 1的双参数形式:

range

答案 1 :(得分:3)

你真的应该使用enumerate这样的东西,因为你可以同时遍历索引和值(这将节省你使用两个for循环的麻烦)。

for i, j in enumerate(list):
    print i, j

您的内部循环覆盖了您在第一个循环中定义的变量index

答案 2 :(得分:0)

for index in range(iteration + 1, len(list))

答案 3 :(得分:0)

请改为尝试:

for index in range(iteration + 1, len(l)):  # don't use "list" as a name

index正在for循环中重新分配,因此index = iteration + 1没有任何效果。

答案 4 :(得分:0)

我赞成 TerryA 的回答。但是我喜欢改进他的答案以满足 OP 要求并使其符合 Python3

他的起始计数器为 1 的示例是:

package strategyRobot;
//import java.awt.datatransfer.SystemFlavorMap;
import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.Queue;
import java.util.Set;
import java.util.LinkedList;
import java.util.List;

public class strategyRobotClass {
    static Set<String> visitedNodes = new HashSet<>();




public static void main(String[] args) {

           int[][] randomMap={
                    { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                    { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
                    { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
                    { 1, 0, 0, 0, 1, 1, 0 ,1, 1, 1 },
                    { 1, 1, 1, 0, 1, 1, 0, 1, 1, 1 },
                    { 1, 1, 1, 0, 1, 1, 0, 1, 1, 1 },
                    { 1, 1, 1, 0, 0, 0, 0, 1, 1, 1 },
                    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
                };
        
        int value = randomMap[2][6];
        System.out.printf("value = %d", value);
        System.out.println(" ");
 
    
        LinkedList<pathCoordinates> pathMap= findPath(randomMap,0,0,3,6);
        for(int i = 0; i<pathMap.size();i++) {
            System.out.printf("(%d,%d) %n",pathMap.get(i).x,pathMap.get(i).y);
            
        }
            
    }

    static public LinkedList<pathCoordinates> findPath(int map[][], int xStartPos, int yStartPos, int xTargetPos, int yTargetPos) {
        LinkedList<pathCoordinates> pathMap = new LinkedList<pathCoordinates>();
        Node source = new Node(xStartPos, yStartPos,0);
        Queue<Node> queue= new LinkedList<Node>();
        queue.add(source);
        while(!queue.isEmpty()) { // IS THE BUG HERE ????
            Node poped = queue.poll();
            if(poped.x == xTargetPos && poped.y ==yTargetPos) {
                return pathMap;
            }
            else {
                map[poped.x][poped.y] = 1 ;
                pathCoordinates coordinates = new pathCoordinates(poped.x,poped.y);
                pathMap.add(coordinates);
                List<Node> neighbourList = addNeighbours(poped,map);
                queue.addAll(neighbourList);
            }
        }
        return null;
    }



static public List addNeighbours(Node poped, int[][] map) {

        List<Node> list=new LinkedList<Node>();
        if((poped.x-1 >0 && poped.x-1<map.length) && (map[poped.x-1][poped.y]== 0)) {
            list.add(new Node(poped.x-1, poped.y, poped.distanceFromStart+1));
        }
        if((poped.x+1 >0 && poped.x+1<map.length) && (map[poped.x+1][poped.y]== 0)) {
            list.add(new Node(poped.x+1, poped.y, poped.distanceFromStart+1));
        }
        
        if((poped.y-1 >0 && poped.y-1<map.length) && (map[poped.x][poped.y-1]== 0)) {
            list.add(new Node(poped.x-1, poped.y, poped.distanceFromStart+1));
        }
        
        if((poped.y+1 >0 && poped.y+1<map.length) && (map[poped.x][poped.y+1]== 0)) {
            list.add(new Node(poped.x, poped.y+1, poped.distanceFromStart+1));
        }
        return list;
        
    }
 static public class Node{
    int x, y; //coordinates in a cell
    int distanceFromStart;
    Node parent;
    Node(int x, int y, int distanceFromStart){
        this.x=x;
        this.y=y;
        this.distanceFromStart=distanceFromStart;
    }
}

static public class pathCoordinates {
    int x,y;
    pathCoordinates(int x, int y){
        this.x=x;
        this.y=y;
    }
    
}

为了更容易理解,我将用下面的内容替换 i 和 j。它可能更好地解释了:(两者都可以,但 i 和 j 代表什么并不明显)

for i, j in enumerate(list,1):
  print (i, j)