tags:

views:

452

answers:

2

Simple question - I am creating a two dim array (ddist = [[0]*d for _ in [0]*d]) using lists in the code below. It outputs distance using gis data. I just want a simple way to take the result of my array/list and output to a text file keeping the same N*N structure. I have used output from print statements in the past but not a good solution in this case.

I am new to python by way of SAS.

def match_bg():
    #as the name sujest this funtion will match the varations of blockgroups with grid travel time. Then output into two arras time and distance. 
    count = -1
    countwo = -1
    ctime = -1
    ddist = [[0]*d for _ in [0]*d] #cratesan N*N array list
    dtime = -1


    while count < 10:
        count = count +1
        #j[count][7] = float(j[count][7])
        #j[count][6] = float(j[count][6])
        while countwo < d:
            countwo = countwo+1
            if count < 1:
                #change values in bg file 
                j[countwo][7] = float(j[countwo][7])
                j[countwo][6] = float(j[countwo][6])




            #print j[count], j[countwo]
            while ctime < RowsT:

                #print ctime,  lenth,  t[ctime][0],  count,  countwo
                ctime = ctime + 1



                #takes both verations of big zone which should be end of the file and matches to travetime file - note 0 and 1 for t[] should be same for different files
                if ((j[count][lenth-1] == t[ctime][0]) and (j[countwo][lenth-1] == t[ctime][1])) or ((j[countwo][lenth-1] == t[ctime][0]) and (j[count][lenth-1] == t[ctime][1])):
                    if t[ctime][0] != t[ctime][1]:
                        #jkdljf
                        x1=3963*j[count][7]*(math.pi/180)
                        x2=3963*j[countwo][7]*(math.pi/180)

                        y1=math.cos(j[count][6]*math.pi/180)*3963*j[count][7]*(math.pi/180)
                        y2=math.cos(j[countwo][6]*math.pi/180)*3963*j[countwo][7]*(math.pi/180)

                        dist=math.sqrt(pow(( x1-x2), 2) +  pow((y1-y2), 2))

                        dtime = dist/t[ctime][11]
                        print countwo,  count
                        ddist[count-1][countwo-1] = dist/t[ctime][lenth]
                        print dtime,  "ajusted time",  "not same grid"

                        print 
                    elif j[count][5] != j[countwo][5]:
                        #ljdkjfs
                        x1=3963*j[count][7]*(math.pi/180)
                        x2=3963*j[countwo][7]*(math.pi/180)

                        y1=math.cos(j[count][6]*math.pi/180)*3963*j[count][7]*(math.pi/180)
                        y2=math.cos(j[countwo][6]*math.pi/180)*3963*j[countwo][7]*(math.pi/180)

                        dist=math.sqrt(pow(( x1-x2), 2) +  pow((y1-y2), 2)) # could change to calculation

                        dtime = (dist/.65)/(t[ctime][10]/60.0)


                        print dtime,  dist, "not in the same bg", j[count], j[countwo],  t[ctime]

                    elif j[count][5] == j[countwo][5]:
                        if t[count][7] < 3000000:
                            dtime = 3
                        elif t[count][7] < 20000000:
                            dtime = 8
                        else:
                            dtime = 12
                        print dtime,  "same bg"
                        print t[ctime][0],   t[ctime],  1,  j[count], j[countwo] 
                    else: 
                        print "error is skip logic",   j[count], j[countwo],  t[ctime]
                    break
                #elif (j[countwo][lenth-1] == t[ctime][0]) and (j[count][lenth-1] == t[ctime][1]):
                    #print t[ctime][0],  t[ctime],  2,   j[count], j[countwo]
                    #break

            ctime = -1

        countwo = -1
A: 

that's what you could to output your 2-d list (or any 2d list for that matter):

with open(outfile, 'w') as file:
    file.writelines('\t'.join(str(j) for j in i) + '\n' for i in top_list)
SilentGhost
A: 

thank a bunch SG