0

以下の形式の辞書があります。

[{'assignedzip_longitude': -71.064699000000005,
  'assignedzip_numberpolygon': u'-71.064483,42.347181 -71.062819,42.346781 -71.061012,42.346099 -71.060932,42.345734 -71.060514,42.345511 -71.060566,42.344569 -71.05971,42.344816 -71.057818,42.346189 -71.05595,42.346601 -71.05683,42.347199 -71.05572,42.34714 -71.052975,42.351364 -71.055483,42.352548 -71.056277,42.35286 -71.056276,42.352891 -71.056476,42.353006 -71.05678,42.353126 -71.056999,42.353208 -71.057292,42.353273 -71.05914,42.35459 -71.06048,42.3555 -71.061772,42.356065 -71.062008,42.355811 -71.062073,42.355276 -71.06287,42.35567 -71.06331,42.35521 -71.064136,42.353623 -71.06423,42.35339 -71.06456,42.35238 -71.063717,42.352295 -71.06258,42.3524 -71.06276,42.35148 -71.063056,42.350902 -71.064275,42.348432 -71.064483,42.347181',
  'assignedzip_pk': u'02111',
  'name': u'Any Name',
  'store_latitude': 42.353197999999999,
  'store_longitude': -71.057495000000003},
 {'assignedzip_latitude': 42.360587000000002,
  'assignedzip_longitude': -71.065447000000006,
  'assignedzip_numberpolygon': u'-71.077051,42.358732 -71.075592,42.355871 -71.075243,42.355601 -71.075054,42.355336 -71.073294,42.355803 -71.073326,42.356427 -71.072439,42.358829 -71.0711,42.358812 -71.071075,42.358278 -71.070669,42.357699 -71.06981,42.35788 -71.070192,42.358828 -71.0677,42.35887 -71.06507,42.35886 -71.065089,42.358327 -71.064355,42.358356 -71.063953,42.358406 -71.062857,42.35865 -71.062873,42.359362 -71.062888,42.361159 -71.061415,42.360908 -71.061495,42.358804 -71.060177,42.359171 -71.060143,42.359052 -71.059765,42.359093 -71.059424,42.359539 -71.05974,42.359735 -71.060275,42.360502 -71.060931,42.360849 -71.060833,42.361341 -71.059718,42.361851 -71.058526,42.362498 -71.057623,42.362647 -71.057993,42.362995 -71.058227,42.364403 -71.058577,42.366966 -71.059112,42.367823 -71.060008,42.369051 -71.063087,42.368704 -71.062968,42.368323 -71.064329,42.369163 -71.064398,42.368983 -71.065072,42.36883 -71.065397,42.368993 -71.067653,42.368983 -71.069793,42.369082 -71.070245,42.368701 -71.070569,42.368283 -71.070816,42.367967 -71.071583,42.368362 -71.071628,42.36829 -71.071625,42.368228 -71.07091,42.367872 -71.075705,42.361646 -71.077051,42.358732',
  'assignedzip_pk': u'02114',
  'name': u'Test Name',
  'store_latitude': 42.357497000000002,
  'store_longitude': -71.058494999999994}]

for ループ内に別の辞書を用意したいのですが、その辞書は次のようになります。

my_dict = {'name': 'Any Name', 'store_lat': 42.353198, 'store_lang': -71.057495, numberpolygon:[[-71.075705, 42.361646],[-71.077051,42.358732],[-71.071625,42.368228]]}....... 

ポリゴン数の値は、スペースで区切る必要があります。

以下のコードで試してみましたが、あまり進歩しませんでした。

for dd in data_dict:
    for val in dd['assignedzip_numberpolygon'].split(" "):                                                                                                                          
        print val

私はpythonとdjangoが初めてなので、解決策を見つけることができませんでした。

4

3 に答える 3

1

主な問題が文字列の分割である場合は、次のように実行できます。

[tuple(map(float, substr.split(','))) for substr in polygon.split(' ')]

ここで、値polygonからコピーした文字列です。assignedzip_numberpolygon数字のペアのリストではなくタプルがあります。形式を変更できる場合は、これをお勧めします。それ以外の場合は、に変更tuple()list()ます(または、Python 2を使用している場合は削除します)。

だからあなたはすることができます:

new_dict = {}
new_dict['name'] = old_dict['name']
new_dict['store_lat'] = old_dict['store_latitude']
new_dict['numberpolygon'] = [tuple(map(float, substr.split(',')))
               for substr in old_dict['assignedzip_numberpolygon'].split(' ')]

..等。リスト内のアイテムに対して実際にこれを行う必要がある場合は、このコードをループ(for old_dict in resultsまたは何か)に入れnew_dict、ループ本体の最後にあるリスト(たとえば)に追加します。

于 2013-01-22T14:33:04.697 に答える
0

分割は比較的簡単です。それを別の関数にします:

import ast

def splitpolygon(poly):
    return [ast.literal_eval(coords) for coords in poly.split()]

これは、ast.literal_eval()関数を使用して、各浮動小数点ペア(コンマで区切られている)をPython浮動小数点値のタプルに変換します。

>>> poly = u'-71.064483,42.347181 -71.062819,42.346781 -71.061012,42.346099 -71.060932,42.345734 -71.060514,42.345511 -71.060566,42.344569 -71.05971,42.344816 -71.057818,42.346189 -71.05595,42.346601 -71.05683,42.347199 -71.05572,42.34714 -71.052975,42.351364 -71.055483,42.352548 -71.056277,42.35286 -71.056276,42.352891 -71.056476,42.353006 -71.05678,42.353126 -71.056999,42.353208 -71.057292,42.353273 -71.05914,42.35459 -71.06048,42.3555 -71.061772,42.356065 -71.062008,42.355811 -71.062073,42.355276 -71.06287,42.35567 -71.06331,42.35521 -71.064136,42.353623 -71.06423,42.35339 -71.06456,42.35238 -71.063717,42.352295 -71.06258,42.3524 -71.06276,42.35148 -71.063056,42.350902 -71.064275,42.348432 -71.064483,42.347181'
>>> splitpolygon(poly)
[(-71.064482999999996, 42.347180999999999), (-71.062819000000005, 42.346781), (-71.061012000000005, 42.346099000000002), (-71.060931999999994, 42.345734), (-71.060513999999998, 42.345511000000002), (-71.060565999999994, 42.344569), (-71.059709999999995, 42.344816000000002), (-71.057817999999997, 42.346189000000003), (-71.055949999999996, 42.346601), (-71.056830000000005, 42.347199000000003), (-71.055719999999994, 42.347140000000003), (-71.052975000000004, 42.351363999999997), (-71.055482999999995, 42.352547999999999), (-71.056276999999994, 42.35286), (-71.056275999999997, 42.352891), (-71.056476000000004, 42.353006000000001), (-71.056780000000003, 42.353126000000003), (-71.056999000000005, 42.353208000000002), (-71.057292000000004, 42.353273000000002), (-71.059139999999999, 42.354590000000002), (-71.060479999999998, 42.355499999999999), (-71.061772000000005, 42.356065000000001), (-71.062008000000006, 42.355811000000003), (-71.062072999999998, 42.355276000000003), (-71.062870000000004, 42.355670000000003), (-71.063310000000001, 42.35521), (-71.064136000000005, 42.353622999999999), (-71.064229999999995, 42.353389999999997), (-71.06456, 42.352379999999997), (-71.063716999999997, 42.352294999999998), (-71.062579999999997, 42.352400000000003), (-71.062759999999997, 42.351480000000002), (-71.063056000000003, 42.350901999999998), (-71.064274999999995, 42.348432000000003), (-71.064482999999996, 42.347180999999999)]

次に、各辞書をループして、その関数をポリゴンに適用する必要があります。

myresults = []
for res in data_dict:
    res_copy = dict(res)
    res_copy['assignedzip_numberpolygon'] = splitpolygon(res_copy['assignedzip_numberpolygon'])
    myresults.append(res_copy)

これにより、を使用して各結果ディクショナリがコピーされるため、新しいリストに保存する前にdict(original)変更(値を置き換える)できます。assignedzip_numberpolygon

于 2013-01-22T14:33:49.067 に答える
0

最も難しい部分は、その文字列を解析することです。これはかなり簡単です。

>>> assignedzip_numberpolygon_str = '-71.064483,42.347181 -71.062819,42.346781 -71.061012,42.346099 -71.060932,42.345734 -71.060514,42.345511 -71.060566,42.344569 -71.05971,42.344816 -71.057818,42.346189 -71.05595,42.346601 -71.05683,42.347199 -71.05572,42.34714 -71.052975,42.351364 -71.055483,42.352548 -71.056277,42.35286 -71.056276,42.352891 -71.056476,42.353006 -71.05678,42.353126 -71.056999,42.353208 -71.057292,42.353273 -71.05914,42.35459 -71.06048,42.3555 -71.061772,42.356065 -71.062008,42.355811 -71.062073,42.355276 -71.06287,42.35567 -71.06331,42.35521 -71.064136,42.353623 -71.06423,42.35339 -71.06456,42.35238 -71.063717,42.352295 -71.06258,42.3524 -71.06276,42.35148 -71.063056,42.350902 -71.064275,42.348432 -71.064483,42.347181'
>>> assignedzip_numberpolygon = [map(float, coord.split(",")) for coord in assignedzip_numberpolygon_str.split()]
>>> assignedzip_numberpolygon
[[-71.064483, 42.347181], [-71.062819, 42.346781], [-71.061012, 42.346099], [-71.060932, 42.345734], [-71.060514, 42.345511], [-71.060566, 42.344569], [-71.05971, 42.344816], [-71.057818, 42.346189], [-71.05595, 42.346601], [-71.05683, 42.347199], [-71.05572, 42.34714], [-71.052975, 42.351364], [-71.055483, 42.352548], [-71.056277, 42.35286], [-71.056276, 42.352891], [-71.056476, 42.353006], [-71.05678, 42.353126], [-71.056999, 42.353208], [-71.057292, 42.353273], [-71.05914, 42.35459], [-71.06048, 42.3555], [-71.061772, 42.356065], [-71.062008, 42.355811], [-71.062073, 42.355276], [-71.06287, 42.35567], [-71.06331, 42.35521], [-71.064136, 42.353623], [-71.06423, 42.35339], [-71.06456, 42.35238], [-71.063717, 42.352295], [-71.06258, 42.3524], [-71.06276, 42.35148], [-71.063056, 42.350902], [-71.064275, 42.348432], [-71.064483, 42.347181]]

次に、dict をコピーして、そのケースを特別に処理するだけです。

def parse_assignedzip_numberpolygon(s):
    return [map(float, coord.split(",")) for coord in s.split()]

def proc_data_dict(d):
    new_d = d.copy()
    new_d['assignedzip_numberpolygon'] = parse_assignedzip_numberpolygon(d['assignedzip_numberpolygon'])
    return new_d

そして、ここに使用例があります:

dataset = [{'assignedzip_longitude': -71.064699000000005,
  'assignedzip_numberpolygon': u'-71.064483,42.347181 -71.062819,42.346781 -71.061012,42.346099 -71.060932,42.345734 -71.060514,42.345511 -71.060566,42.344569 -71.05971,42.344816 -71.057818,42.346189 -71.05595,42.346601 -71.05683,42.347199 -71.05572,42.34714 -71.052975,42.351364 -71.055483,42.352548 -71.056277,42.35286 -71.056276,42.352891 -71.056476,42.353006 -71.05678,42.353126 -71.056999,42.353208 -71.057292,42.353273 -71.05914,42.35459 -71.06048,42.3555 -71.061772,42.356065 -71.062008,42.355811 -71.062073,42.355276 -71.06287,42.35567 -71.06331,42.35521 -71.064136,42.353623 -71.06423,42.35339 -71.06456,42.35238 -71.063717,42.352295 -71.06258,42.3524 -71.06276,42.35148 -71.063056,42.350902 -71.064275,42.348432 -71.064483,42.347181',
  'assignedzip_pk': u'02111',
  'name': u'Any Name',
  'store_latitude': 42.353197999999999,
  'store_longitude': -71.057495000000003},
 {'assignedzip_latitude': 42.360587000000002,
  'assignedzip_longitude': -71.065447000000006,
  'assignedzip_numberpolygon': u'-71.077051,42.358732 -71.075592,42.355871 -71.075243,42.355601 -71.075054,42.355336 -71.073294,42.355803 -71.073326,42.356427 -71.072439,42.358829 -71.0711,42.358812 -71.071075,42.358278 -71.070669,42.357699 -71.06981,42.35788 -71.070192,42.358828 -71.0677,42.35887 -71.06507,42.35886 -71.065089,42.358327 -71.064355,42.358356 -71.063953,42.358406 -71.062857,42.35865 -71.062873,42.359362 -71.062888,42.361159 -71.061415,42.360908 -71.061495,42.358804 -71.060177,42.359171 -71.060143,42.359052 -71.059765,42.359093 -71.059424,42.359539 -71.05974,42.359735 -71.060275,42.360502 -71.060931,42.360849 -71.060833,42.361341 -71.059718,42.361851 -71.058526,42.362498 -71.057623,42.362647 -71.057993,42.362995 -71.058227,42.364403 -71.058577,42.366966 -71.059112,42.367823 -71.060008,42.369051 -71.063087,42.368704 -71.062968,42.368323 -71.064329,42.369163 -71.064398,42.368983 -71.065072,42.36883 -71.065397,42.368993 -71.067653,42.368983 -71.069793,42.369082 -71.070245,42.368701 -71.070569,42.368283 -71.070816,42.367967 -71.071583,42.368362 -71.071628,42.36829 -71.071625,42.368228 -71.07091,42.367872 -71.075705,42.361646 -71.077051,42.358732',
  'assignedzip_pk': u'02114',
  'name': u'Test Name',
  'store_latitude': 42.357497000000002,
  'store_longitude': -71.058494999999994}]

for d in dataset:
    new_d = proc_data_dict(d)
    print new_d

出力は次のとおりです。

{'store_latitude': 42.353198, 'name': u'Any Name', 'assignedzip_numberpolygon': [[-71.064483, 42.347181], [-71.062819, 42.346781], [-71.061012, 42.346099], [-71.060932, 42.345734], [-71.060514, 42.345511], [-71.060566, 42.344569], [-71.05971, 42.344816], [-71.057818, 42.346189], [-71.05595, 42.346601], [-71.05683, 42.347199], [-71.05572, 42.34714], [-71.052975, 42.351364], [-71.055483, 42.352548], [-71.056277, 42.35286], [-71.056276, 42.352891], [-71.056476, 42.353006], [-71.05678, 42.353126], [-71.056999, 42.353208], [-71.057292, 42.353273], [-71.05914, 42.35459], [-71.06048, 42.3555], [-71.061772, 42.356065], [-71.062008, 42.355811], [-71.062073, 42.355276], [-71.06287, 42.35567], [-71.06331, 42.35521], [-71.064136, 42.353623], [-71.06423, 42.35339], [-71.06456, 42.35238], [-71.063717, 42.352295], [-71.06258, 42.3524], [-71.06276, 42.35148], [-71.063056, 42.350902], [-71.064275, 42.348432], [-71.064483, 42.347181]], 'assignedzip_longitude': -71.064699, 'store_longitude': -71.057495, 'assignedzip_pk': u'02111'}
{'store_latitude': 42.357497, 'name': u'Test Name', 'assignedzip_numberpolygon': [[-71.077051, 42.358732], [-71.075592, 42.355871], [-71.075243, 42.355601], [-71.075054, 42.355336], [-71.073294, 42.355803], [-71.073326, 42.356427], [-71.072439, 42.358829], [-71.0711, 42.358812], [-71.071075, 42.358278], [-71.070669, 42.357699], [-71.06981, 42.35788], [-71.070192, 42.358828], [-71.0677, 42.35887], [-71.06507, 42.35886], [-71.065089, 42.358327], [-71.064355, 42.358356], [-71.063953, 42.358406], [-71.062857, 42.35865], [-71.062873, 42.359362], [-71.062888, 42.361159], [-71.061415, 42.360908], [-71.061495, 42.358804], [-71.060177, 42.359171], [-71.060143, 42.359052], [-71.059765, 42.359093], [-71.059424, 42.359539], [-71.05974, 42.359735], [-71.060275, 42.360502], [-71.060931, 42.360849], [-71.060833, 42.361341], [-71.059718, 42.361851], [-71.058526, 42.362498], [-71.057623, 42.362647], [-71.057993, 42.362995], [-71.058227, 42.364403], [-71.058577, 42.366966], [-71.059112, 42.367823], [-71.060008, 42.369051], [-71.063087, 42.368704], [-71.062968, 42.368323], [-71.064329, 42.369163], [-71.064398, 42.368983], [-71.065072, 42.36883], [-71.065397, 42.368993], [-71.067653, 42.368983], [-71.069793, 42.369082], [-71.070245, 42.368701], [-71.070569, 42.368283], [-71.070816, 42.367967], [-71.071583, 42.368362], [-71.071628, 42.36829], [-71.071625, 42.368228], [-71.07091, 42.367872], [-71.075705, 42.361646], [-71.077051, 42.358732]], 'assignedzip_longitude': -71.065447, 'store_longitude': -71.058495, 'assignedzip_latitude': 42.360587, 'assignedzip_pk': u'02114'}
于 2013-01-22T14:38:43.860 に答える