简体   繁体   中英

Creating a 2d list from lots of 1d lists python

Hi all I am having trouble with creating a 2d list from lots of 1d lists. My code reads as follows

  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

When i print comicdb i get the following results.

[' 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']

What i would like to know is. Is there a code that can be added so i can combine all these single lists into a double list such that my code looks like this when printed.

[[' 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']]

Essentially i want all these individual lists to be combined into one double list so that I can use the double list to access each list individually. I want to print each list separtely later on in the code.

In python, you can append a list to a list.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM