从列表中提取特定数据条目(作为组)并存储在单独的列表中?

时间:2013-02-26 23:02:42

标签: python list nested-loops

例如,这是我正在循环的列表示例, ['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->发送到不同的列表。

棘手的部分是列表内部并不总是具有相同的数据集,'groups'可以是任意大小和位置。唯一的定义是它们被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

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

希望很清楚,任何人都可以伸出援手吗?

3 个答案:

答案 0 :(得分:3)

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]

答案 1 :(得分: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]]

答案 2 :(得分:0)

这有效:

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更快,更容易,经过验证。用那个。

相关问题