【问题标题】:Get indices of roughly equal sized chunks获取大致相等大小的块的索引
【发布时间】:2015-11-23 19:22:59
【问题描述】:

这个问题与Python: Slicing a list into n nearly-equal-length partitions非常相似。

但是,我不想实际分割列表。我想要的只是每个块的开始和停止索引值,就像我在切片列表一样。

所以我想要一个接受输入的函数:

def partitionIndexes( totalsize, numberofpartitions):

并返回表示每个分区的开始和结束索引的元组列表。每个元组应该跨越大致相同数量的索引(在 1 以内)。

例子:

>>>partitionIndexes( 105, 10 )
[(0, 10)
(11, 21)
(22, 32)
(33, 43)
(44, 54)
(55, 64)
(65, 74)
(75, 84)
(85, 94)
(95, 104)]

注意前五个分区如何跨越 11 个索引,而后五个分区如何跨越 10 个索引。

如果可能,我想避免生成所有索引的中间列表。

【问题讨论】:

    标签: python list slice


    【解决方案1】:

    你可以用一个简单的生成器函数来做到这一点。

    def partitionIndexes(totalsize, numberofpartitions):
        # Compute the chunk size (integer division; i.e. assuming Python 2.7)
        chunksize = totalsize / numberofpartitions
        # How many chunks need an extra 1 added to the size?
        remainder = totalsize - chunksize * numberofpartitions
        a = 0
        for i in xrange(numberofpartitions):
            b = a + chunksize + (i < remainder)
            # Yield the inclusive-inclusive range
            yield (a, b - 1)
            a = b
    

    【讨论】:

      【解决方案2】:

      我的简单实现将this answer 扩展到链接的问题并滥用内置的reduce。

      def partition(totalsize, n):
          lst = range(totalsize)
          chunks = [lst[i::n] for i in xrange(n)]
          indecies = reduce(lambda x, y: reducechunks(x, y), chunks)
          return indecies
      
      
      def reducechunks(listoftuples, nextchunk):
          if listoftuples[0] == 0:
              # This is the first tuple, need to add it to the list
              listoftuples = [(0, len(listoftuples)-1)]
      
          # Start of this tuple is the end of the last one plus 1
          start = listoftuples[-1][1] + 1
      
          # End of this tuple is the start plus 1 minus the length of the current chunk
          end = start + len(nextchunk) - 1
      
          # Append this tuple to the list of tuples to be passed to the next iteration
          listoftuples.append((start, end))
          return listoftuples
      

      此实现的一个限制是它会生成所有索引的列表。

      【讨论】:

        【解决方案3】:

        这是一个返回切片的解决方案。

        def partition_chunks(total_size: int, chunk_size: int)->List[slice]:
            # Create a list of start indices
            chunk_slice = slice(0, total_size, chunk_size)
            values = range(0, total_size)
            start_indices = values[chunk_slice]
        
            # Create start,end] slices
            slices: List[slice] = [slice(start, start+chunk_size) for start in start_indices]
        
            # Fix the last partition as end_index of the last slice should be "total_size"
            slices[-1] = slice(start_indices[-1], total_size)
        
            return slices
        

        例子:

        slices: List[slice] = partition_chunks(total_size=1111, chunk_size=100)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-12
          • 2017-03-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-19
          • 1970-01-01
          相关资源
          最近更新 更多