大家好我从很多1d列表创建2d列表时遇到了麻烦。我的代码如下:
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']]
基本上我希望将所有这些单独的列表组合成一个双列表,以便我可以使用双列表分别访问每个列表。我想稍后在代码中单独打印每个列表。
答案 0 :(得分: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