Python递归索引更改

时间:2018-12-27 22:35:11

标签: python-3.x list recursion matrix

我正在尝试安排矩阵以使其动态更改索引。

我试图通过for循环来实现它,但是对于每个索引它只做一次。

def arrangeMatrix(progMatrix):
    for l in range(len(progMatrix)):
        for item in range(len(progMatrix[l])):
            if indexExists(progMatrix,l + 1,item) and progMatrix[l + 1][item] == " ":
                progMatrix[l + 1][item] = progMatrix[l][item]
                progMatrix[l][item] = " "

原始列表是:

1   0       7   6   8
0   5           5   5
2   1               6
4   1   3   7        
1   1   1   7       5

我的代码应该从上到下填充所有空白索引,但是我的结果是:

1   0           6   8
0   5               5
2   1       7        
4   1   3   7       6
1   1   1   7   5   5

实际结果应该是:

1   0               
0   5               8
2   1       7       5
4   1   3   7   6   6
1   1   1   7   5   5

任何帮助或提示,我们都会感激。

1 个答案:

答案 0 :(得分:0)

如果您首先对列进行迭代,则可能会更容易,因为在一列中发生的更改与其他列中发生的变化无关。然后,对于每一列,您可以从下到上迭代单元格,并跟踪下一个非空间应“下降”到的y坐标。

不需要递归。

以下是该代码的编码方式:

<link href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.theme.default.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="owl-carousel owl-theme">
  <div class="item">
    <div class="item-inner-wrapper">
      <h4>1</h4>
      <div class="details">
        This is some crazy cool details that you will have to know about
      </div>
    </div>
  </div>
  <div class="item">
    <div class="item-inner-wrapper">
      <h4>2</h4>
      <div class="details">
        This is some crazy cool details that you will have to know about
      </div>
    </div>
  </div>
  <div class="item">
    <div class="item-inner-wrapper">
      <h4>3</h4>
      <div class="details">
        This is some crazy cool details that you will have to know about
      </div>
    </div>
  </div>
  <div class="item">
    <div class="item-inner-wrapper">
      <h4>4</h4>
      <div class="details">
        This is some crazy cool details that you will have to know about
      </div>
    </div>
  </div>
  <div class="item">
    <div class="item-inner-wrapper">
      <h4>5</h4>
      <div class="details">
        This is some crazy cool details that you will have to know about
      </div>
    </div>
  </div>
  <div class="item">
    <div class="item-inner-wrapper">
      <h4>6</h4>
      <div class="details">
        This is some crazy cool details that you will have to know about
      </div>
    </div>
  </div>
  <div class="item">
    <div class="item-inner-wrapper">
      <h4>7</h4>
      <div class="details">
        This is some crazy cool details that you will have to know about
      </div>
    </div>
  </div>
  <div class="item">
    <div class="item-inner-wrapper">
      <h4>8</h4>
      <div class="details">
        This is some crazy cool details that you will have to know about
      </div>
    </div>
  </div>
  <div class="item">
    <div class="item-inner-wrapper">
      <h4>9</h4>
      <div class="details">
        This is some crazy cool details that you will have to know about
      </div>
    </div>
  </div>
  <div class="item">
    <div class="item-inner-wrapper">
      <h4>10</h4>
      <div class="details">
        This is some crazy cool details that you will have to know about
      </div>
    </div>
  </div>
  <div class="item">
    <div class="item-inner-wrapper">
      <h4>11</h4>
      <div class="details">
        This is some crazy cool details that you will have to know about
      </div>
    </div>
  </div>
  <div class="item">
    <div class="item-inner-wrapper">
      <h4>12</h4>
      <div class="details">
        This is some crazy cool details that you will have to know about
      </div>
    </div>
  </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js"></script>