2017-05-16 24 views
2

セットアップ繰り返し処理<a href="https://scrapy.org/" rel="nofollow noreferrer">Scrapy</a>を使用して文字列

間の範囲を通して、私は住宅の広告をこするいます。住宅広告ごとに、私は郵便番号を取得します。

私は、全体の辞書はhereを表示することができ

postal_district = {'A': ['1011AB', '1011BD', '1011BG', '1011CE', 
         '1011CH', '1011CZ', '1011DB', '1011DD']} 

、地区に郵便番号をリンク辞書を持っています。

リスト内の次の2つの郵便番号は、範囲を形成します。最初の郵便番号は範囲の最小値で、2番目の郵便番号は最大値です。

など。

'1011AB', '1011AC',...,'1011AZ', '1011BA',...,'1011BD'

のいずれかの郵便番号は、地区'A'に属します。

私の目標は、郵便番号と辞書を使って地区に広告をマッチングさせることです。


問題

私は前の質問hereを求めてきましたし、問題を解決するために、このanswerに従うことを選択しました。

したがって、私はこの作品いくつかの郵便番号については

def is_in_postcode_range(current_postcode, min, max): 
    return min <= current_postcode <= max 

def get_district_by_post_code(postcode): 
    for district, codes in postal_district.items(): 
     first_code = codes[0] 
     last_code = codes[-1] 
     if is_in_postcode_range(postcode, first_code, last_code): 
      if any(is_in_postcode_range(postcode, codes[i], codes[i+1]) for i in range(0, len(codes), 2)): 
       return district 
      else: 
       return None 

district = get_district_by_post_code(pc) 

、地区に広告を一致させるために、次のコードを使用しています。ただし、多くの郵便番号は一致しません。 1035CK,1072LL,1059ECなどがあります。


どうしたのですか?それは辞書かコードですか?

私は辞書をソートしました。

答えて

1

この構築:

if is_in_postcode_range(postcode, first_code, last_code): 
    if any(is_in_postcode_range(postcode, codes[i], codes[i+1]) 
      for i in range(0, len(codes), 2)): 
     return district 
    else: 
     return None 

は、郵便地区が重複する範囲を持っていないことを前提としています。それが真実でない場合は、削除する必要があります。

else: 
    return None 
+0

私は気になるでしょう。 – LucSpan

関連する問題