0

そのため、一連のファイルを読み取り、必要なデータを追加するプログラムがあります。これらの特定のデータを取得して、リストとして表示する必要があります。より具体的には、これらは私が持っているパラメータです:

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)
4

1 に答える 1

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
于 2013-11-18T00:02:39.217 に答える