嗨,我正在尝试对词典列表进行排序,并使用attrgetter进行排序,但显示的是attrgetter尚未定义。
>>> a = ['{"count": 1, "places": [{"lat": 12.99455070495605, "lng": 77.6714096069336, "address": "Karnataka, India"}], "created_at": "2015-03-17 04:25:10", "updated_at": "", "environments": [{"display_address": "Karnataka, India", "name": "", "views": 0, "country": "India", "region": "Karnataka", "address": "4th Cross Street, Mottappa Compound, A Narayanapura, Mahadevapura, Bengaluru, Karnataka 560016, India", "comments": 0, "height": 758, "width": 1008, "image_url": "http://9cb448a50dcef7c3d28e-10dfb1f9ec7a490e45082ae9c51f52eb.r27.cf1.rackcdn.com/photos/4185354/ff90ae45348134c1345c49b2c853a31e.jpg", "likes": 0, "photo_where": "", "lat": 12.99455070495605, "photo_type": "panorama", "lng": 77.6714096069336, "thumb_url": "http://0f9cb149e5cc61745fd5-2870fdbe0c753e4bc418911d9614db21.r17.cf1.rackcdn.com/thumbs/534901/", "tiles_url": "http://c6c0b88e96acc3346aff-c40106bef0a12d7839dc46608748511d.r69.cf1.rackcdn.com/tiles/534901/", "id": 534901, "is_spherical": 0, "source": {"fov": 123.867, "scale": 0.0, "startx": 0, "vertical_fov": 39.4, "starty": 0}}], "user": {"profile_pic_url": "http://22d39cd6accfe73dc8bf-8a3f41b0dadcd18fc1e3dc2022064b19.r29.cf1.rackcdn.com/panographers/panographers_default_07S.png", "id": 4185354, "profile_url": "http://www.teliportme.com/profile/moto.teliportme", "name": "moto.teliportme"}, "photo_type": "panorama", "type": "upload"}', '{"count": 1, "places": [{"lat": 12.99455547332764, "lng": 77.67140197753906, "address": "Karnataka, India"}], "created_at": "2015-03-17 04:25:19", "updated_at": "2015-03-18 06:22:08", "environments": [{"address": "4th Cross Street, Mottappa Compound, A Narayanapura, Mahadevapura, Bengaluru, Karnataka 560016, India", "height": 750, "is_spherical": 0, "likes": 1, "lng": 77.67140197753906, "id": 534902, "user_id": 4185354, "source": {"fov": 138.302, "scale": 0.0, "startx": 0, "vertical_fov": 39.4, "starty": 0}, "tags": [{"category": "admin", "environment_id": 534902, "name": "popular", "tag_id": 15138}], "comments": 0, "width": 1273, "tiles_url": "http://71d4e0cc9d7caffc7e21-1f1149a3ba6cc477dd90c37b0398406a.r26.cf1.rackcdn.com/tiles/534902/", "display_address": "Karnataka, India", "views": 0, "photo_where": "", "lat": 12.99455547332764, "photo_type": "panorama", "thumb_url": "http://30f287b674c32ae6d7d0-4c458530dcb1ef77c889ef5a5d185cb3.r35.cf1.rackcdn.com/thumbs/534902/", "name": "", "country": "India", "region": "Karnataka", "image_url": "http://72dd3f16a93e55ea7eb9-e4ef3340595b2ac7356e5ba4ff95e48d.r89.cf1.rackcdn.com/photos/4185354/b771316a647b04413e5a7bd09497582d.jpg"}], "user": {"profile_pic_url": "http://22d39cd6accfe73dc8bf-8a3f41b0dadcd18fc1e3dc2022064b19.r29.cf1.rackcdn.com/panographers/panographers_default_07S.png", "id": 4185354, "profile_url": "http://www.teliportme.com/profile/moto.teliportme", "name": "moto.teliportme"}, "photo_type": "panorama", "type": "upload"}']
>>> a = sorted(a, key=attrgetter('likes'), reverse=True)
最佳答案
attrgetter
是operator
模块中stdlib的一部分,而不是内置函数。因此,您需要import attrgetter
from operator
.
from operator import attrgetter