用列表中的项目排序列表

2024-04-27 23:06:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个清单'd8'有3个项目

>>> d8[0]
{u'lix_edu_dev_override': u'control', u'fmt__school_highlight': u'Indian Institute of Social Welfare & Business Administration, Kolkata', u'degree': u'Master of Business Administration (MBA)', u'i18n__detailed_school_page': u'More details for this school', u'lix_treasury_upload': u'B', u'fosList': [{u'link__fos_pivot': u'/search?search=&keywords=Marketing&sortCriteria=R&keepFacets=true&trk=prof-edu-field_of_study', u'fmt__fos_highlight': u'Marketing', u'name': u'Marketing', u'i18n__find_others': u'Find users with this keyword'}], u'enddate_iso': u'1993', u'fieldOfStudy': u'Marketing', u'link__peer_tracker_pivot_with_name': u'/college/?eduSchool=Indian+Institute+of+Social+Welfare+%26+Business+Administration%2C+Kolkata&trk=prof-edu-school-name', u'educationId': 134727074, u'fmt__degree_highlight': u'Master of Business Administration (MBA)', u'i18n__other_alumni': u'Find other members who attended Indian Institute of Social Welfare & Business Administration, Kolkata', u'startdate_iso': u'1991', u'schoolName': u'Indian Institute of Social Welfare & Business Administration, Kolkata', u'link__school_logo': u'/edu/school', u'lix_edu_profile_link': u'control', u'startdate_my': u'1991', u'enddate_my': u'1993', u'lix_edu_mvp_override': u'control'}

>>> d8[1]
{u'lix_edu_dev_override': u'control', u'link__peer_tracker_pivot_with_id': u'/college/?eduSchool=20479&trk=prof-edu-school-name', u'degree': u'B.Sc.', u'i18n__detailed_school_page': u'More details for this school', u'lix_treasury_upload': u'B', u'fosList': [{u'link__fos_pivot': u'/search?search=&keywords=physics&sortCriteria=R&keepFacets=true&trk=prof-edu-field_of_study', u'fmt__fos_highlight': u'physics', u'name': u'physics', u'i18n__find_others': u'Find users with this keyword'}], u'enddate_iso': u'1991', u'fieldOfStudy': u'physics', u'lix_edu_profile_link': u'control', u'schoolId': 20479, u'educationId': 15866086, u'fmt__degree_highlight': u'B.Sc.', u'i18n__other_alumni': u"Find other members who attended St. Xavier's College", u'fmt__school_highlight': u"St. Xavier's College", u'startdate_iso': u'1988', u'schoolName': u"St. Xavier's College", u'link__school_logo': u'/edu/school?id=20479', u'startdate_my': u'1988', u'enddate_my': u'1991', u'lix_edu_mvp_override': u'control'}

>>> d8[2]
{u'lix_edu_dev_override': u'control', u'fmt__school_highlight': u"St. Xavier's School Loyola Hall", u'degree': u'H.S.C.', u'i18n__detailed_school_page': u'More details for this school', u'lix_treasury_upload': u'B', u'fosList': [{u'link__fos_pivot': u'/search?search=&keywords=Science&sortCriteria=R&keepFacets=true&trk=prof-edu-field_of_study', u'fmt__fos_highlight': u'Science', u'name': u'Science', u'i18n__find_others': u'Find users with this keyword'}], u'enddate_iso': u'1988', u'fieldOfStudy': u'Science', u'link__peer_tracker_pivot_with_name': u'/college/?eduSchool=St%2E+Xavier%27s+School+Loyola+Hall&trk=prof-edu-school-name', u'educationId': 15978673, u'fmt__degree_highlight': u'H.S.C.', u'i18n__other_alumni': u"Find other members who attended St. Xavier's School Loyola Hall", u'startdate_iso': u'1976', u'schoolName': u"St. Xavier's School Loyola Hall", u'link__school_logo': u'/edu/school', u'lix_edu_profile_link': u'control', u'startdate_my': u'1976', u'enddate_my': u'1988', u'lix_edu_mvp_override': u'control'}

我想按“enddate\u iso”或“startdate\u iso”对列表进行排序(升序)。有可能吗?你知道吗


Tags: ofnamelinkisobusinesscontroli18nedu
1条回答
网友
1楼 · 发布于 2024-04-27 23:06:11

试一试:

>>> from operator import itemgetter
>>> sorted(d8, key=itemgetter('startdate_iso'))

sorted生成一个新列表。如果要就地排序,请使用sort()

>>> d8.sort(key=itemgetter('startdate_iso'))

相关问题 更多 >