简体   繁体   中英

Python sort a list of objects/dictionaries with a given sortKey function

(I use Python 2 here)

I have a list of dictionaries, say

dei = [{'name': u'Thor'}, {'name': u'Œdipus'}, {'name': u'Creon'}]

I would like to sort that list by their 'name' attribute. This is easily done so:

dei.sort(key=lambda d: d['name'])

Now, because Python's alphabetical sorting is ASCII-driven, the result will be

[{'name': u'Creon'}, {'name': u'Thor'}, {'name': u'Œdipus'}]

while I'd like Œdipus to be between Creon and Thor.

Following this suggestion , I use PyICU's collator.getSortKey() function (let's rename it sortKey() for readability), which works this way on a list strings of strings:

strings.sort(key=sortKey)

My problem here: as I cannot modify the sortKey() function anyhow, how can I use it to sort a list of more complex objects (here, dictionaries) according to some attribute?

The only way I found for the moment is by extracting the values of the dictionary in a separate list, sorting it, then implementing a custom compare(a, b) function returning −1, 0 or 1 depending on the index of a and b in the separate list, and calling sort() with this compare() function:

names = sorted([d['name'] for d in dei], key=sortKey)

def compare(a, b):
    if names.index(a) < names.index(b):
        return -1
    elif names.index(a) > names.index(b):
        return 1
    else:
        return 0

results = dei.sort(key=lambda d: d['name'], cmp=compare)

which I don't find very elegant.

You can use your own key that internally calls getSortKey with correct value:

>>> import icu
>>> dei = [{'name': u'Thor'}, {'name': u'Œdipus'}, {'name': u'Creon'}]
>>> collator = icu.Collator.createInstance()
>>> dei.sort(key=lambda x: collator.getSortKey(x['name']))
>>> dei
[{'name': 'Creon'}, {'name': 'Œdipus'}, {'name': 'Thor'}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM