0

選挙マップを提供する大きな geoJSON ファイルがあります。私はサイトをスクレイピングし、有権者選挙区の結果を次のような Python 辞書に返しました{u'605': [u'56', u'31'], u'602': [u'43', u'77']:

辞書の結果を使用して、geoJSON ファイル (すべての有権者地区) を更新したいと考えています。私の geoJSON ファイルには、キーと値のペア ( - など"precNum": 602) の 1 つとして地区番号があります。ディクショナリの結果で各形状を更新するにはどうすればよいですか?

次のようなものを使用して、geoJSON ファイルをターゲットにしてループできます。

for precincts in map_data["features"]:
    placeVariable = precincts["properties"]

    placeVariable["precNum"] 
    #This gives me the precinct number of the current shape I am in.

    placeVariable["cand1"] = ?? 
    # I want to add the Value of first candidate's vote here

    placevariable["cand2"] = ?? 
    # I want to add the Value of second candidate's vote here

どんなアイデアでも大きな助けになるでしょう。

4

2 に答える 2

1

こんな感じで更新できます。

your_dict = {u'605': [u'56', u'31'], u'602': [u'43', u'77']}

for precincts in map_data["features"]:

    placeVariable = precincts["properties"]
    prec = placeVariable["precNum"] 

    if your_dict.get(prec): #checks if prec exists in your_dict
        placeVariable["cand1"] = your_dict['prec'][0]
        placevariable["cand2"] = your_dict['prec'][0]
于 2013-10-07T23:55:35.763 に答える