2012-11-22 95 views
2

我想遍歷列表,並根據幾個條件來重新排列列表中的項目,所有內部原始列表是。所以用下面的代碼在Python,而列表1打印正確的分組0,1S和2S:通過Python中的嵌套列表迭代

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

一切都得到了第二個名單試圖通過獲得重新組進一步子列表作爲燒烤銫上混合起來:

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(); 
+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']]] 

你的輸出要求是有點令人費解;如果返回分組元素的列表是不夠的,呼籲groupedlist()是不夠的:

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

    [[[i] for i in v] for v in list1] 
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']]]