迭代python中的嵌套列表

时间:2012-11-22 17:06:35

标签: python list nested loops

我正在尝试遍历列表,并根据几个条件重新排列列表中列表中的项目,所有这些都在原始列表中。所以使用Python中的下面的代码,而list1通过分组0s,1s和2s正确打印:

new list 1 = [['A0', 'B0', 'C0'], ['A1', 'B1', 'C1'], ['A2', 'B2', 'C2']]

所有内容都在第二个列表中混淆,试图通过获取更多的子列表作为Bs Cs重新分组:

new list 2 = [[['A0', 'A1', 'A2'], ['B0', 'B1', 'B2'], ['C0', 'C1', 'C2']]]

期待得到:

new list 1 = [[['A0'], ['B0'], ['C0']], [['A1'], ['B1'], ['C1']], [['A2'], ['B2'], ['C2']]]

下面是我使用的代码(抱歉长度,我确定有一个非常简化的版本)。

def Main():

    array = ['A0', 'A1', 'A2', 'B0', 'B1', 'B2', 'C1', 'C0', 'C2']

    list1 = NewList1(array)
    print 'new list 1 = ' + str(list1)

    list2 = NewList2(list1)
    print 'new list 2 = ' + str(list2)

def NewList1(thelist):
    sublist1=[]
    sublist2=[]
    sublist3=[]
    sublist4=[]
    newlist=[]
    for item in thelist:
        if hasattr(item,"__iter__"):
            for subitem in item:
                sublist1.append(subitem)
        elif "0" in item:
                sublist2.append(item)
        elif "1" in item:
                sublist3.append(item)
        elif "2" in item:
                sublist4.append(item)
        else:
            newlist.append(item)
    if sublist2: newlist.append(sublist2)
    if sublist3: newlist.append(sublist3)
    if sublist4: newlist.append(sublist4)

    if sublist1: newlist.append(NewList1(sublist1))

    return newlist


def NewList2(thelist):
    sublist1=[]
    sublist2=[]
    sublist3=[]
    sublist4=[]
    newlist=[]
    for item in thelist:
        if hasattr(item,"__iter__"):
            for subitem in item:
                sublist1.append(subitem)
        elif "A" in item:
                sublist2.append(item)
        elif "B" in item:
                sublist3.append(item)
        elif "C" in item:
                sublist4.append(item)
        else:
            newlist.append(item)
    if sublist2: newlist.append(sublist2)
    if sublist3: newlist.append(sublist3)
    if sublist4: newlist.append(sublist4)

    if sublist1: newlist.append(NewList2(sublist1))

    return newlist

Main();

3 个答案:

答案 0 :(得分:3)

按分组条件排序,然后使用itertools.groupby()

from itertools import groupby

key = lambda el: el[1]
list2 = [[[el] for el in grouped] for group, grouped in groupby(sorted(input, key=key), key)]

演示:

>>> from itertools import groupby
>>> input = ['A0', 'A1', 'A2', 'B0', 'B1', 'B2', 'C1', 'C0', 'C2']
>>> key = lambda el: el[1]
>>> [[[el] for el in grouped] for group, grouped in groupby(sorted(input, key=key), key)]
[[['A0'], ['B0'], ['C0']], [['A1'], ['B1'], ['C1']], [['A2'], ['B2'], ['C2']]]

您的输出要求有点复杂;如果返回分组元素的列表已足够,则在list()上调用grouped即可:

>>> [list(grouped) for group, grouped in groupby(sorted(input, key=key), key)]
[['A0', 'B0', 'C0'], ['A1', 'B1', 'C1'], ['A2', 'B2', 'C2']]

答案 1 :(得分:1)

     python 3.2

     [[[i] for i in v] for v in list1]

答案 2 :(得分:0)

考虑more_itertools.chunked,它接受​​可迭代和块大小n

import more_itertools as mit

data = ['A0', 'A1', 'A2', 'B0', 'B1', 'B2', 'C1', 'C0', 'C2']

list(mit.chunked(data, 3))
# [['A0', 'A1', 'A2'], ['B0', 'B1', 'B2'], ['C1', 'C0', 'C2']]

list(mit.chunked(list(mit.chunked(data, 1)), 3))
# [[['A0'], ['A1'], ['A2']], [['B0'], ['B1'], ['B2']], [['C1'], ['C0'], ['C2']]]