2014-03-05 96 views
0

我有一段代碼打印清單表

resultDict = {} 
for startCity in places: 
    for endCity in places: 
     startLocation = startCity[0] + ' ' + startCity[1] 
     endLocation = endCity[0] + ' ' + endCity[1] 
     resultLocations = [startLocation, endLocation] 
     resultId = ','.join(resultLocations.sort()) 

     if startLocation == endLocation or resultId in resultDict: 
      continue 

     dlat = math.radians(float(startCity[2])-float(endCity[2])) 
     dlon = math.radians(float(startCity[3])-float(endCity[3])) 
     a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(float(endCity[2]))) \ 
      * math.cos(math.radians(float(startCity[2]))) * math.sin(dlon/2) * math.sin(dlon/2) 
     c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) 
     d = radius * c 
     resultDict[resultId] = d * 0.621371 

print resultDict 

使用print語句時產生以下屏幕:

(0.0, (['Millersville CA'], ['Millersville CA']), 0.0) 
(1626.614065545485, (['Millersville CA'], ['Millersville IL']), 1616.3457942569537) 
(1789.8845362720158, (['Millersville CA'], ['Millersville IN']), 1778.7968284940225) 
(2295.8829666956335, (['Millersville CA'], ['Millersville MD']), 2281.198586905769) 
(1595.039113682296, (['Millersville CA'], ['Millersville MO']), 1584.3029694701652) 
(2082.089905489436, (['Millersville CA'], ['Millersville NC']), 2068.1505623183266) 
(1937.855421683643, (['Millersville CA'], ['Millersville OH']), 1927.2582650063941) 
(2304.425345768765, (['Millersville CA'], ['Millersville PA']), 2290.4979215838316) 
(1771.801667686284, (['Millersville CA'], ['Millersville TN']), 1759.8880673544743) 
(2106.8263361464506, (['Millersville CA'], ['Millersville WV']), 2093.504423623986) 
(1838.7000061834806, (['Millersville CA'], ['Millersville MI']), 1832.9430747114486) 
(1626.614065545485, (['Millersville IL'], ['Millersville CA']), 1616.3457942569537) 
(0.0, (['Millersville IL'], ['Millersville IL']), 0.0) 
(165.46706149552824, (['Millersville IL'], ['Millersville IN']), 164.45127349792836) 
(669.3166714199295, (['Millersville IL'], ['Millersville MD']), 664.8533524270723) 
(143.7059918008934, (['Millersville IL'], ['Millersville MO']), 144.75572344683556) 

它是什麼做的是計算一堆之間的距離城市,然後產生結果作爲英里的距離的列表中,那麼這兩個城市的名稱,然後這兩個城市作爲一種檢查之間的勾股定理距離。我怎麼會去格式化打印語句打印結果出來是這樣的:

我想這是打印出來的屏幕上,所以我將如何去使用print和正確地在格式化它將列標題作爲城市,然後爲英里輸入正確的號碼?

我想到的是這個代碼並不完全符合我的要求......它只吐出第一對城市,然後移動到下一對,並將它們打印在第一對下方:

print "\n\n    {0:^}".format(resultDict2),"\n\n{0:^}".format(resultDict3)," {0:^12.5}".format(resultDict4),"\nGC   {0:^22.5}".format(resultDict1) 

這裏的印結的幾個例子:

   Millersville MI 

Millersville TN  553.22  
GC     549.55   


      Millersville MI 

Millersville WV  434.49  
GC     433.64   


      Millersville MI 

Millersville MI  0.0  
GC     0.0 

有沒有辦法讓代碼打印下一對城市的最後一權,而不是樓下?

我欣賞的幫助......我一直停留在這過去幾天,似乎無法得到它的工作。


編輯:

被問及places ...這裏是什麼地方出了進去:

245939,Millersville,CA,Kern,35.30385,-118.45786,767,2516,Loraine,6,29,-2005993.585,-7850.609146 
422981,Millersville,IL,Christian,39.45115,-89.15814,200,656,Owaneco,17,21,582900.2782,239679.282 
439235,Millersville,IN,Marion,39.8531,-86.09165,228,748,Indianapolis East,18,97,838665.4307,307491.2101 
590805,Millersville,MD,Anne Arundel,39.05955,-76.64802,47,154,Odenton,24,3,1647634.706,343137.5611 
722401,Millersville,MO,Cape Girardeau,37.43283,-89.79954,140,459,Millersville,29,31,543086.0153,10195.50234 
1021470,Millersville,NC,Alexander,35.85569,-81.18342,293,961,Millersville,37,3,1320855.874,-81032.00324 
1048977,Millersville,OH,Sandusky,41.312,-83.28131,212,696,Helena,39,143,1053471.336,497736.4912 
1181260,Millersville,PA,Lancaster,39.99788,-76.35413,99,325,Conestoga,42,71,1650717.971,451187.4979 
1293977,Millersville,TN,Sumner,36.37116,-86.71,154,505,Goodlettsville,47,165,824583.7325,-86143.9105 
1555125,Millersville,WV,Marion,39.4612,-80.14813,330,1083,Fairmont West,54,49,1345239.636,332269.94 
1617722,Millersville,MI,Wexford,44.27667,-85.55978,425,1394,Boon,26,165,829275.484,803975.6286 
+1

的數據結構。你有一個列表元組,但是這個元組(城市)中的所有列表都只有一個元素! – msvalkon

+0

@msvalkon:我該如何解決這個問題? – Tony

+0

'「」。加入(resultLocations.sort())'應該誤差'list.sort()'不返回任何 – cmd

回答

1

首先,我會解決你的數據結構(它有點搞砸)

resultDict = {} 
cities = [' '.join(c[:2]) for c in places] 
for startCity in places: 
    for endCity in places: 
     start = ' '.join(startCity[:2]) 
     end = ' '.join(endCity[:2]) 
     resultDict[start, end] = calc_distance(startCity, endCity) 

calc_distance返回一個列表(正常和GC)的兩個距離。像

def calc_distance(startCity, endCity): 
    # whatever magic you need to do to calculate your distances 
    return [distance, gc_distance] 

現在resultDict應該像

{ 
    ('Millersville CA', 'Millersville CA'): [0.0, 0.0], 
    ('Millersville CA', 'Millersville IL'): [1616.3457942569537, 1626.614065545485], 
    ('Millersville CA', 'Millersville IN'): [1778.7968284940225,1789.8845362720158], 
    ('Millersville CA', 'Millersville MD'): [2281.198586905769,2295.8829666956335], 
    ('Millersville CA', 'Millersville MO'): [1584.3029694701652, 1595.039113682296], 
    ('Millersville CA', 'Millersville NC'): [2068.1505623183266, 2082.089905489436], 
    .... 
} 

然後打印出來是沒有意義的你的工作更容易

print "\n" 
f = "{:>22}" * (len(cities) + 1) 
print f.format("", *cities) 
for c in cities: 
    print 
    print f.format(c, *[resultDict.get((c, e))[0] for e in cities]) 
    print f.format("[GC]", *[resultDict.get((c, e))[1] for e in cities]) 
+0

我得到一個錯誤:join()只需要一個參數(給出2)的行城市= [''.join(c [0],c [1])for c in places] – Tony

+0

你可以發佈整個代碼你曾經得到你在那裏發佈的resultDict ...我只是使用Python 2.7獲取錯誤 – Tony

+0

@Tony抱歉,修正了加入 – cmd