在python中创建数据列的列

时间:2013-03-06 22:38:14

标签: python list

所以我有一个程序,它读取一堆文件并附加我需要的必要数据。我现在需要获取这些特定数据并将其显示为列表。更具体地说,这些是我的参数:

a =来源,b =发光度,c =发光度误差,d = HST,e = XRS,f = gmag,g = z,h = rh

我想在列表中显示它,每个列表定义一个特定的列。我只是不知道我应该在各种for循环中插入print语句到底是做什么的。

我将不胜感激任何帮助!这是程序(主要关注的是完成的for循环以及它们如何遍历数据,并且不担心缩进,到目前为止程序工作我只需要显示附加在列中的数据):

    import sys
    import os 
    import re
    import urllib
    import urllib2
    from os.path import basename
    import urlparse 
    import shutil


    base_dirname = '/projects/XRB_Web/apmanuel/499/'
    base_sourcefile = base_dirname + 'Sources.txt'

    try:
      file = open(base_sourcefile, 'r')
    except IOError:
      print 'Cannot open: '+base_sourcefile

    Source = []
    Finallist =  [] 
    ACS = [] 
    SRC = []


    for line in file: 
      data_line_check = (line.strip())
      if data_line_check: 

        line =  re.sub(r'\s+', ' ', line)
        point  = line.split('|')
        temp_source =  (point[0]).strip()
        if temp_source and len(point) == 3:
              Source = (point[0]).strip() 
              Source = re.sub(r'\s', '_', Source)
              print Source+"\n" 

        temp_finallist = (point[1]).strip()
        if temp_finallist: 
             Finallistaddress = (point[1]).strip()
             Finallistaddress = re.sub(r'\s', '_', Finallistaddress) 
             Luminositybase_dirname1 = '/projects/XRB_Web/apmanuel/499/Lists/' + Finallistaddress


     try:
       file2 = open(Luminositybase_dirname1, 'r')
     except IOError:
       print 'Cannot open: '+Luminositybase_dirname1

     source = []
     luminosity = []
     luminosityerr = []

     for line in file2: 
         pointy = line.split()
     a = int(pointy[0])
         b = float(pointy[5])
         c = float(pointy[6])

         source.append(a)
         luminosity.append(b)
         luminosityerr.append(c)

        temp_HST = (point[2]).strip()
        if temp_HST: 
          HSTaddress = (point[2]).strip()
          HSTaddress = re.sub(r'\s', '_', HSTaddress) 
          HSTbase_dirname2 = '/projects/XRB_Web/apmanuel/499/Lists/' + HSTaddress


     try:
       file3 = open(HSTbase_dirname2, 'r')
     except IOError:
       print 'Cannot open: '+HSTbase_dirname2

     HST = []

     for line in file3: 
     pointy2 = line.split()
     d = int(pointy2[0])
         HST.append(d)

      temp_XRS = (point[3]).strip()
      if temp_XRS: 
        XRSaddress = (point[3]).strip()
        XRSaddress =re.sub(r'\s', '_', XRSaddress) 
        XRSbase_dirname3 = '/projects/XRB_Web/apmanuel/499/Lists/' + XRSaddress


     try:
       file4 = open(XRSbase_dirname3, 'r')
     except IOError:
       print 'Cannot open: '+XRSbase_dirname3

     XRS = []

     for line in file4: 
     pointy3 = line.split() 
     e = int(pointy3[0])
         XRS.append(e)

    temp_others = (point[4]).strip()
    if temp_others: 
       othersaddress = (point[4]).strip()
       othersaddress =re.sub(r'\s', '_', othersaddress) 
       othersbase_dirname4 = '/projects/XRB_Web/apmanuel/499/Lists/' + othersaddress

     try:
       file5 = open(othersbase_dirname4, 'r')
     except IOError:
       print 'Cannot open: '+othersbase_dirname4

     gmag = []
     z = []
     rh = []

     for line in file5: 
     pointy4 = line.split()
     f = float(pointy4[3])
         g = float(pointy4[5])
         h = float(pointy4[7])
         rh.append(f)
         gmag.append(g)
         z.append(h)

1 个答案:

答案 0 :(得分:0)

此函数将返回行列表的列。请注意,这要求所有列表都在您尝试访问的列中有一个元素,但如果需要,可以相对简单地更改它。

def getcolumn(matrix,index):        #index specifies which column of the matrix you want. note that like all other list indexes, this starts from 0, not one.
    column = []
    for row in matrix:
        column.append(row[index])
    return column