我有这个对象列表
event
第一个元素和第二个元素具有相同的纬度和经度。我想将它们组合在一起,最终结果将是
[
{'latitude': 40.7136948694776, 'days': "['MON']", 'from_time': '11AM', 'longtitude': -73.9869100091687, 'to_time': '12:30PM', 'signdesc1': 'NO PARKING (SANITATION BROOM SYMBOL) 11AM TO 12:30PM MON & THURS'},
{'latitude': 40.7136948694776, 'days': "['MON']", 'from_time': '11AM', 'longtitude': -73.9869100091687, 'to_time': '12:30PM', 'signdesc1': 'NO PARKING (SANITATION BROOM SYMBOL) 11AM TO 12:30PM MON & FRI'},
{'latitude': 40.7136948694776, 'days': "['MON']", 'from_time': '11AM', 'longtitude': -73.9869100091688, 'to_time': '12:30PM', 'signdesc1': 'NO PARKING (SANITATION BROOM SYMBOL) 11AM TO 12:30PM MON & WED'},
]
我怎样才能以pythonic 3的方式做到这一点?
答案 0 :(得分:2)
这不是python3特有的,但它无需修改即可使用:
>>> from collections import defaultdict
>>> d=defaultdict(list)
>>> for item in a:
... d[(item['latitude'],item['longtitude'])].append(item)
...
>>> d
defaultdict(<type 'list'>, {(40.7136948694776, -73.9869100091688): [{'days': "['MON']", 'longtitude': -73.9869100091688, 'signdesc1': 'NO PARKING (SANITATION BROOM SYMBOL) 11AM TO 12:30PM MON & WED', 'to_time': '12:30PM', 'latitude': 40.7136948694776, 'from_time': '11AM'}], (40.7136948694776, -73.9869100091687): [{'days': "['MON']", 'longtitude': -73.9869100091687, 'signdesc1': 'NO PARKING (SANITATION BROOM SYMBOL) 11AM TO 12:30PM MON & THURS', 'to_time': '12:30PM', 'latitude': 40.7136948694776, 'from_time': '11AM'}, {'days': "['MON']", 'longtitude': -73.9869100091687, 'signdesc1': 'NO PARKING (SANITATION BROOM SYMBOL) 11AM TO 12:30PM MON & FRI', 'to_time': '12:30PM', 'latitude': 40.7136948694776, 'from_time': '11AM'}]})
>>> d.values()
[[{'days': "['MON']", 'longtitude': -73.9869100091688, 'signdesc1': 'NO PARKING (SANITATION BROOM SYMBOL) 11AM TO 12:30PM MON & WED', 'to_time': '12:30PM', 'latitude': 40.7136948694776, 'from_time': '11AM'}], [{'days': "['MON']", 'longtitude': -73.9869100091687, 'signdesc1': 'NO PARKING (SANITATION BROOM SYMBOL) 11AM TO 12:30PM MON & THURS', 'to_time': '12:30PM', 'latitude': 40.7136948694776, 'from_time': '11AM'}, {'days': "['MON']", 'longtitude': -73.9869100091687, 'signdesc1': 'NO PARKING (SANITATION BROOM SYMBOL) 11AM TO 12:30PM MON & FRI', 'to_time': '12:30PM', 'latitude': 40.7136948694776, 'from_time': '11AM'}]]