0

こんにちは、たくさんの 1 次元リストから 2 次元リストを作成するのに問題があります。私のコードは次のようになります

  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 つの二重リストに結合して、二重リストを使用して各リストに個別にアクセスできるようにしたいと考えています。後でコード内で各リストを個別に印刷したいと思います。

4

1 に答える 1

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
于 2013-05-29T06:55:37.523 に答える