【问题标题】:How to prepare batches of data from a list of values? [duplicate]如何从值列表中准备批量数据? [复制]
【发布时间】:2021-04-18 06:58:19
【问题描述】:

这是我的清单,

data = (i for i in list("abcdefghijklmnopqrstuvwxyzabcedefghijklmnopqrstuvwxyz"))

这里data 是一个生成器,我想对其进行迭代并准备一批 12 个相等的数据点,如果最后一批中少于 12 个,我也需要它,但下面的代码不起作用,

subsets = []
subset = []
for en, i in enumerate(data):
    if en % 12 == 0 and en > 0:
        subsets.append(subset)
        subset = []
    else:
        subset.append(i)

print(subsets)

[['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'],
 ['n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x'],
 ['z', 'a', 'b', 'c', 'e', 'd', 'e', 'f', 'g', 'h', 'i'],
 ['k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u']]

但是我的代码不能正常工作,因为第一个嵌套列表有 12 个值,但其余的有 11 个值,并且错过了最后一批中小于 12 个的最后几个值

预期输出:

[['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'],
 ['m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x'],
 ['y', 'z', 'a', 'b', 'c', 'e', 'd', 'e', 'f', 'g', 'h', 'i'],
 ['j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u'],
 ['v', 'w', 'x', 'y', 'z']]

【问题讨论】:

  • 顺便说一句,你本来可以写data = iter("abc...")
  • 剩余子列表只有 11 个值的简单原因是您没有在 en % 12 == 0 and en > 0 情况下附加值。

标签: python python-3.x


【解决方案1】:

两处变化,需要从1开始迭代,并在清空之前追加到子列表中:

data = (i for i in list("abcdefghijklmnopqrstuvwxyzabcedefghijklmnopqrstuvwxyz"))
subsets = []
subset = []
# start counting from index '1'
for en, i in enumerate(data, 1):
    if en % 12 == 0 and en > 0:
        # append the current element before emptying 'subset'
        subset.append(i)
        subsets.append(subset)
        subset = []
    else:
        subset.append(i)
# append the left-over sublist/subset to your main list as well
subsets.append(subset)

for i in subsets:
    print(i)

给予

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l']
['m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x']
['y', 'z', 'a', 'b', 'c', 'e', 'd', 'e', 'f', 'g', 'h', 'i']
['j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u']
['v', 'w', 'x', 'y', 'z']

【讨论】:

    【解决方案2】:

    替代解决方案是使用内置itertools.islice。您可以查看哪种方法更快或更方便。氪

    import itertools
    
    def gen_sublist(your_iter, size):
        while True:
            part = tuple(itertools.islice(your_iter, size))
            if not part:
                break
            yield part
    
    data = (i for i in list("abcdefghijklmnopqrstuvwxyzabcedefghijklmnopqrstuvwxyz"))
    
    for c in gen_sublist(data, size=12):
        print(c)
    

    返回:

    ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l')
    ('m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x')
    ('y', 'z', 'a', 'b', 'c', 'e', 'd', 'e', 'f', 'g', 'h', 'i')
    ('j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u')
    ('v', 'w', 'x', 'y', 'z')
    

    【讨论】:

      【解决方案3】:

      不使用模数或枚举的不同方法(只是另一种选择,因为其他答案已经纠正了您的方法):

      In [1]: subsets = []                                                                                                                 
                                                                                                                                            
      In [2]: data = (i for i in list("abcdefghijklmnopqrstuvwxyzabcedefghijklmnopqrstuvwxyz"))                                            
      
      In [3]:
          ...: while True:                                                                                                                  
          ...:     try: 
          ...:         x = [] 
          ...:         for i in range(12): 
          ...:             x.append(next(data)) 
          ...:         subsets.append(x) 
          ...:     except: # Catch StopIteration Exception when generator runs out of values
          ...:         subsets.append(x) 
          ...:         break 
          ...:                                                                                                                              
      

      输出:

      In [4]: subsets                                                                                                                      
      Out[4]:                                                                                                                              
      [['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'],
       ['m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x'],
       ['y', 'z', 'a', 'b', 'c', 'e', 'd', 'e', 'f', 'g', 'h', 'i'],
       ['j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u'],
       ['v', 'w', 'x', 'y', 'z']]
      
      

      【讨论】:

        猜你喜欢
        • 2015-01-19
        • 2014-03-11
        • 2019-12-02
        • 2016-03-27
        • 2019-08-10
        • 1970-01-01
        • 2016-12-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多