计算2D阵列中的出现次数

时间:2019-10-22 16:21:31

标签: python

假设我有一个像这样的数组

grid: 
    [[1, 1, 0, 0, 0],
    [1, 1, 0, 0, 0],
    [0, 0, 1, 0, 0],
    [0, 0, 0, 1, 1]]

在这种情况下,我想隔离“项目”的组,它们是三个组,规则是0,用于像交集一样将它们分开。因此,此示例有3组,每组1个。

如果您知道如何使用python做到这一点,那么我会被问到的第一个问题是,我尝试将自己的作业不交给社区的证明是什么,我当时的想法是反复下移,然后离开可能会错过一些数字,因为如果您考虑一下,它将形成一个从左上角开始的十字架,这个小组很好的在这里学习。 因此,对于我和对这种数据科学感兴趣的其他人(如问题)要体贴。

2 个答案:

答案 0 :(得分:1)

如果您不需要知道哪些集合是重复项,则可以使用python的set内置函数来确定列表中的唯一项。由于setlist的{​​{1}}中不起作用,因此这可能会有些棘手。但是,您可以将其转换为listlist,将它们放回tuple,然后获取该列表的list,以找出有多少个唯一值集。

len

答案 1 :(得分:0)

connected component labeling的基于深度优先搜索的相对简单的方法。

<!DOCTYPE html>
<html lang="en">

<head>
   <meta charset="UTF-8" />
   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
   <meta http-equiv="X-UA-Compatible" content="ie=edge" />
   <link rel="stylesheet" type="text/css" href="style.css" />
   <link href="https://fonts.googleapis.com/css?family=Press+Start+2P|Roboto:700&display=swap" rel="stylesheet">
   <title>Legend of Zelda: Quiz</title>
</head>

<body>
   <div class="pagecontainer">

      <main>

         <div class="count border">
            <header>
               <div class="headercontainer potCounter">
                  <br>
                  <h1>Are you Worthy of the Master Sword?</h1>
               </div>
            </header>
         </div>

         <div class="health border">
            <section>
               <div class="heartcontainer">
                  <img src=images/full.png class='empty'>
                  <img src=images/full.png class='empty'>
                  <img src=images/full.png class='empty'>
               </div>
            </section>
         </div>

         <div class="quiz border background-image">
            <section>
               <div id="quizcontainer">
                  <div class="startcontainer">
                     <span>
                        Test your knowlodge on "The Legend Zelda Series" to see if you are worthy of
                        wielding the Master Sword!
                     </span>
                     <br>
                     <br>
                     <button class="press startbutton">Start</button>
                  </div>
            </section>
         </div>
      
      </main>

      <footer>
         
         <div class="centercontainer">
            <div class="border">
               <ul class="contactcontainer">
                  <li><a href="mailto:#" target="_top"><img src="images/email.png"
                           class="contactinfo" alt="Email Icon" /></a></li>
                  <li><a href="#" target="_blank"><img src="images/webpage.png" class="contactinfo"
                           alt="Website Icon" /></a></li>
                  <li><a href="#" target="_blank"><img
                           src="images/linkedin.png" class="contactinfo" alt="LinkedIn Icon" /></a></li>
               </ul>
            </div>
         </div>
      
      </footer>

      <script src="https://code.jquery.com/jquery-3.4.1.js"
         integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
      <script src="STORE.js"></script>
      <script src="script.js"></script>

   </div>
</body>

</html>

示例输入def get_components(grid, indicator=1): def label(g, row, col, group): if row >= 0 and col >= 0 and row < len(g) and col < len(g[row]) and g[row][col] == -1: # only label if currently unlabeled g[row][col] = group # attempt to label neighbors with same label label(g, row + 1, col, group) label(g, row, col + 1, group) label(g, row - 1, col, group) label(g, row, col - 1, group) return True else: return False # initialize label grid as -1 for entries that need labeled label_grid = [[-1 if gc == indicator else 0 for gc in gr] for gr in grid] group_count = 0 for row, grid_row in enumerate(grid): for col in range(len(grid_row)): if label(label_grid, row, col, group_count + 1): group_count += 1 return label_grid, group_count 的结果是

label_grid, group_count = get_components(grid)

对于以下情况

label_grid = [[1, 1, 0, 0, 0],
              [1, 1, 0, 0, 0],
              [0, 0, 2, 0, 0],
              [0, 0, 0, 3, 3]]
group_count = 3

我们得到grid = [[1 0 1], [1 1 1]]