2013-05-29 82 views
0

大家好,我很難從很多1d列表創建2d列表。我的代碼如下所示從很多1d列表創建2d列表python

lis=[average, average1, average2, average3, average4, average5, average6, average7, average8, average9] 
    totalaverage= 3 


    print 
    print "Station 1 = Station 0 from menu. Please read all other stations accordingly" 
    print 
    for i,x in enumerate(lis): 
    if x < totalaverage: 
    aboveaverage = " {} average is less than {}".format(i+1,totalaverage) 


    option = 0 
    comicdb = [] 

    record = {} 
    record = aboveaverage 
    comicdb.append(record) 
    print comicdb 


    elif x > totalaverage: 
    belowaverage = "{} average is greater than {}".format(i+1,totalaverage) 
    print belowaverage 

當我打印comicdb時,我得到以下結果。

[' 1 average is less than 3'] 
[' 2 average is less than 3'] 
[' 3 average is less than 3'] 
[' 4 average is less than 3'] 
[' 5 average is less than 3'] 
[' 6 average is less than 3'] 
[' 7 average is less than 3'] 
[' 8 average is less than 3'] 
[' 9 average is less than 3'] 
[' 10 average is less than 3'] 

我想知道的是。有沒有可以添加的代碼,所以我可以將所有這些單列表合併到一個雙列表中,以便在打印時我的代碼看起來像這樣。

[[' 1 average is less than 3'], 
[' 2 average is less than 3'], 
[' 3 average is less than 3'], 
[' 4 average is less than 3'], 
[' 5 average is less than 3'], 
[' 6 average is less than 3'], 
[' 7 average is less than 3'], 
[' 8 average is less than 3'], 
[' 9 average is less than 3'], 
[' 10 average is less than 3']] 

基本上我希望所有這些單獨的列表合併成一個雙列表,以便我可以使用雙列表來單獨訪問每個列表。我想稍後在代碼中單獨列出每個列表。

回答

1

在python中,你可以追加一個列表到列表。

new_list = [] 
for i,x in enumerate(lis): 
if x < totalaverage: 
    aboveaverage = " {} average is less than {}".format(i+1,totalaverage) 
    option = 0 
    comicdb = [] 
    record = {} 
    record = aboveaverage 
    comicdb.append(record) 
    new_list.append(comicdb) 
    print comicdb 

    elif x > totalaverage: 
     belowaverage = "{} average is greater than {}".format(i+1,totalaverage) 
     print belowaverage 
print new_list 
+0

謝謝Rawrgulmuffins非常感謝 – user2430623