2017-04-02 49 views
0

這是我的代碼,我知道它不夠乾淨,但它不能像我想的那樣運行,任何人都可以幫助我?從計數功能得到錯誤的結果

我固定它,謝謝您的幫助

這段代碼的總體思路是比較一個範圍內的每個座標,並計算積分的金額爲每個網格

結果是: ''其他':862,'B4':2,'D5':1,'A1':0,'D4':2,'A3':2,'A2':2,'A4':0, 'B2':36,'B3':8,'C3':15,'C2':50,'C1':1,'C4':7,'D3':4,'C5':1,'B1 ':7}

我不知道爲什麼'其他'得到這麼多

import json 
    from pprint import pprint 
    xa1_min = 144.700000 
    xa1_max = 144.850000 
    ya1_min = -37.650000 
    ya1_max = -37.500000 

    xa2_min = 144.850000 
    xa2_max = 145.000000 
    ya2_min = -37.650000 
    ya2_max = -37.500000 

    xa3_min = 145.000000 
    xa3_max = 145.150000 
    ya3_min = -37.650000 
    ya3_max = -37.500000 

    xa4_min =145.150000 
    xa4_max =145.300000 
    ya4_min =-37.650000 
    ya4_max =-37.500000 

    xb1_min =144.700000 
    xb1_max =144.850000 
    yb1_min =-37.800000 
    yb1_max =-37.650000 

    xb2_min =144.850000 
    xb2_max =145.000000 
    yb2_min =-37.800000 
    yb2_max =-37.650000 

    xb3_min =145.000000 
    xb3_max =145.150000 
    yb3_min =-37.800000 
    yb3_max =-37.650000 

    xb4_min =145.150000 
    xb4_max =145.300000 
    yb4_min =-37.800000 
    yb4_max =-37.650000 

    xc1_min =144.700000 
    xc1_max =144.850000 
    yc1_min =-37.950000 
    yc1_max =-37.800000 

    xc2_min =144.850000 
    xc2_max =145.000000 
    yc2_min =-37.950000 
    yc2_max =-37.800000 

    xc3_min =145.000000 
    xc3_max =145.150000 
    yc3_min =-37.950000 
    yc3_max =-37.800000 

    xc4_min =145.150000 
    xc4_max =145.300000 
    yc4_min = -37.950000 
    yc4_max = -37.800000 

    xc5_min =145.300000 
    xc5_max =145.450000 
    yc5_min =-37.950000 
    yc5_max =-37.800000 

    xd3_min =145.000000 
    xd3_max =145.150000 
    yd3_min =-38.100000 
    yd3_max =-37.950000 

    xd4_min =145.150000 
    xd4_max =145.300000 
    yd4_min =-38.100000 
    yd4_max =-37.950000 

    xd5_min =145.300000 
    xd5_max =145.450000 
    yd5_min =-38.100000 
    yd5_max =-37.950000 
    #["properties"]['ID'] 
    # Reading from grid 
    with open('tinyTwitter.json') as load_f: 
     info = json.load(load_f) 

    coordinates = list() 
    #print info 

    for line in info: 
     #print line['json']['coordinates']['coordinates'] 
     coordinates.append(line['json']['coordinates']['coordinates']) 

    #print coordinates[0][1] 
    #print coordinates[0][0] 

    counts = {'A1':0,'A2':0,'A3':0,'A4':0,'B1':0,'B2':0,'B3':0,'B4':0,'C1':0,'C2':0,'C3':0,'C4':0,'C5':0,'D3':0,'D4':0,'D5':0,'other':0} 
    #comparing 
    def compare(x,y): 


    if xa1_min <= x <= xa1_max and ya1_min <= y <= ya1_max: 
     counts['A1'] +=1 

    elif xa2_min <= x <= xa2_max and ya2_min <=y<= ya2_max: 
     counts['A2'] +=1 

    elif xa3_min <= x <= xa3_max and ya3_min <=y<= ya3_max: 
     counts['A3'] +=1 

    elif xa4_min <= x <= xa4_max and ya4_min <=y<= ya4_max: 
     counts['A4'] += 1 

    elif xb1_min <= x <= xb1_max and yb1_min <=y<= yb1_max: 
     counts['B1'] += 1 

    elif xb2_min <= x <= xb2_max and yb2_min <=y<= yb2_max: 
     counts['B2'] += 1 

    elif xb3_min <= x <= xb3_max and yb3_min <=y<= yb3_max: 
     counts['B3'] += 1 

    elif xb4_min <= x <= xb4_max and yb4_min <=y<= yb4_max: 
     counts['B4'] += 1 

    elif xc1_min <= x <= xc1_max and yc1_min <=y<= yc1_max: 
     counts['C1'] += 1 

    elif xc2_min <= x <= xc2_max and yc2_min <=y<= yc2_max: 
     counts['C2'] += 1 

    elif xc3_min <= x <= xc3_max and yc3_min <=y<= yc3_max: 
     counts['C3'] += 1 

    elif xc4_min <= x <= xc4_max and yc4_min <=y<= yc4_max: 
     counts['C4'] += 1 

    elif xc5_min <= x <= xc5_max and yc5_min <=y<= yc5_max: 
     counts['C5'] += 1 

    elif xd3_min <= x <= xd3_max and yd3_min <=y<= yd3_max: 
     counts['D3'] += 1 

    elif xd4_min <= x <= xd4_max and yd4_min <=y<= yd4_max: 
     counts['D4'] += 1 

    elif xd5_min <= x <= xd5_max and yd5_min <=y<= yd5_max: 
     counts['D5'] += 1 

    else: 
     counts['other']+= 1 

     #print counts 

    for i,j in coordinates: 
     #print i,j 
     compare(i,j) 
    print counts 
+0

顯然,0 + 0 = 0 – Mox

+0

我懷疑你的剩餘問題可能與你嵌套的'if's有關。如果先前的內部「if」失敗,則不會檢查附加到頂級「if」的'elif'。您可能希望將'x'和'y'條件與'和'結合起來,而不是嵌套'if'語句。你也可以通過像xa1_min Blckknght

+0

仍然有一些問題,以執行不正確的結果 – hsbzzhz

回答

0

也許你正在尋找:

len(counts.keys()) 

如果不是這樣,一些更多的上下文周邊您的問題將有助於找到解決辦法。此外,它看起來像是可以通過將最小/最大數據點存儲在字典列表中並將這些比較語句壓縮成單個循環來節省相當多的代碼重複。沿線的東西:

data = [{'x-min': 0, 'x-max': 1, 'y-min': 0, 'y-max': 1, 'count': 0}, ...] 

def within_threshold(x1, y1, point): 
    within_x = x >= point['x-min'] and x <= point['x-max'] 
    within_y = y >= point['y-min'] and y <= point['y-max'] 
    return within_x and within_y 

def compare(x, y): 
    for point in data: 
     if within_threshold(x, y, point): 
      point['count'] += 1 
+1

是的,那我試圖表達,謝謝! – hsbzzhz

1

與形式的線替換形式

counts['A1']+=counts['A1'] 

的線

counts['A1']+=1 

正因爲如此,你永遠不增加counts因爲你把它的當前值( 0)每一次。