【问题标题】:Extract specific data entries(as groups) from a list and store in separate lists?从列表中提取特定数据条目(作为组)并存储在单独的列表中?
【发布时间】:2013-02-26 23:02:42
【问题描述】:

例如,这是我正在循环的列表的示例, ['n', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 , 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 'n', 'n', 'n', 'n', 'n', ' n','n','n','n','n','n','n','n','n','n','n','n','n' ,'n','n','n','n','n','n','n','n','n','n','n','n',' n', 'n', 'n', 'n', 'n', 82, 83, 84, 85, 86, 87, 88, 'n', 'n', 'n', 'n', ' n','n','n','n','n','n','n','n','n','n','n','n','n' ,'n','n','n','n','n','n','n','n','n','n','n','n',' n', 'n', 'n', 'n', 'n', ''n', 'n', 'n', 178, 179, 180]

这个列表是从之前调用的函数生成的(n 已被插入以隐藏不需要的值)。

我正在尝试将在 n 之间分隔的数字分组并将它们发送到一个列表,例如将数字 1-37-> 放入一个列表,将数字 82-88-> 不同的列表,178-180 -> 发送到不同的列表。

棘手的部分是列表中并不总是包含相同的数据集,“组”可以是任意大小和位置。唯一的定义特征是它们由 n 分隔。

到目前为止我的尝试:

for i in range(0, len(lists)):
        for index, item in enumerate(lines):
            if item != 'n': #if item is not n send to list
                lists[i].append(item)           
            elif lines[index+1] == 'n':#if the next item is an n
                 del lines[:index]

“lists”实际上是在此函数之外创建的列表列表,为了存储每个组,列表的数量取决于需要存储的组数。

'lines' 是我希望循环的值列表。

我的逻辑是,所有不是“n”的值都附加到第一个列表中,如果下一个值是 n,则删除之前的所有值并循环遍历新列表,将下一组值放入下一个列表。很快。

除了我得到:列表索引超出范围

我明白,但我希望有办法绕过它。我也尝试在 elif 处跳出循环,但后来我无法继续我离开的循环。 我的最后一次尝试是在第一次运行后设置的位置重新启动循环,如下所示:

place=0
    for i in range(0, len(lists)):
        for index, item in enumerate(lines[place:]):
            if item != 'n': #if item is not n send to list
                lists[i].append(item)           
            elif lines[index+1] == 'n':#if the next item is an n
                 place=[index]
                 break

切片索引必须是整数或 None 或具有 index 方法

希望这很清楚,有人可以帮忙吗?

【问题讨论】:

    标签: python list nested-loops


    【解决方案1】:
    from itertools import groupby
    
    data =  ['n', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 82, 83, 84, 85, 86, 87, 88, 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 178, 179, 180]
    
    def keyfunc(n):
      return n == 'n'
    
    groups = [list(g) for k, g in groupby(data, keyfunc) if not k]
    

    【讨论】:

      【解决方案2】:
      >>> import itertools
      >>> data = ['n', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 82, 83, 84, 85, 86, 87, 88, 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 178, 179, 180]
      >>> [list(g) for k, g in itertools.groupby(data, lambda x: x != 'n') if k]                                                                                                
      [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37],
       [82, 83, 84, 85, 86, 87, 88],
       [178, 179, 180]]
      

      【讨论】:

        【解决方案3】:

        这行得通:

        li=['n', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 82, 83, 84, 85, 86, 87, 88, 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 178, 179, 180]
        
        from itertools import groupby
        
        def is_n(c): return c=='n'
        
        print [list(t1) for t0,t1 in groupby(li, is_n) if t0==False]
        

        打印:

        [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37], [82, 83, 84, 85, 86, 87, 88], [178, 179, 180]]
        

        如果你想这样做'原始'(没有 itertools),这可行:

        def is_n(c): return c=='n'
        
        def divide(li, f):
            r=[]
            while li:
                while li and f(li[0]):
                    li.pop(0)
                sub=[]
                while li and not f(li[0]):
                    sub.append(li.pop(0))
                r.append(sub)
            return r
        
        print divide(li,is_n) 
        

        或者,如果你想使用 for 循环:

        def divide4(li,f):
            r=[]
            sub=[]
            for e in li:
                if f(e):
                    if len(sub)==0:
                         continue
                    else:
                        r.append(sub)
                        sub=[]    
                else:
                    sub.append(e)
            else:
                if len(sub): r.append(sub)
            return r  
        
        print divide4(li,is_n)        
        

        无论哪种情况,打印:

        [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37], [82, 83, 84, 85, 86, 87, 88], [178, 179, 180]]
        

        itertools 更快、更简单、经过验证。用那个。

        【讨论】:

          猜你喜欢
          • 2021-01-14
          • 2021-04-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-24
          • 2019-10-19
          • 2016-07-01
          • 2022-01-05
          相关资源
          最近更新 更多