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(' ')]

我从valuepolygon复制的字符串在哪里。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(' ')]

..ETC。如果您确实需要对列表中的项目执行此操作,请将此代码放入循环(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 回答