简体   繁体   中英

Python: Sorting a list of dictionaries by a value from a list of tuples in the dictionary

I'm have trouble with lists and dictionaries. I have a list of dictionaries of skiers. Similar to below:

skier_1 = {'id': 123,
           'first_name': 'John',
           'last_name': 'Smith',
           'times': [('race_1', 32.25), ('race_2', 33.5), ('race_3', 44)]}

skier_2 = {'id': 234,
           'first_name': 'Allison',
           'last_name': 'Anderson',
           'times': [('race_1', 29.5), ('race_2', 41), ('race_3', 40.25)]}

skier_3 = {'id': 456,
           'first_name': 'Bob',
           'last_name': 'Johnson',
           'times': [('race_1', 31), ('race_2', 41), ('race_3', 39.75)]}

skiers = [skier_1, skier_2, skier_3]

I am supposed to write a function to return a list of the n skier dictionaries with the fastest time on the race passed in. If there is a tie, it should be broken by skier id.

def fastest_n_times(skiers, race_name, n):

I'm getting confused on how to sort the list by a value from a tuple in a list in the dictionary skier_x. My plan was to sort the list completely then just return the n highest. I can sort by skier id pretty easily.

def fastest_n_times(skiers, race_name, n):
   by_id = []
   from operator import itemgetter
   by_id = sorted(skiers, key=itemgetter('id'))

However, getting the times out of the dictionary. I have no idea. I've tried:

for skier in skiers:
    fastest_skiers = sorted(skiers, key=itemgetter(sort_key)(itemgetter('assignments')(skier)))

I know this loop isn't the right way. However, this returns an error TypeError: 'tuple' object is not callable . I know tuples are immutable, but I don't know why it is not callable?

def fastest_n_times(skiers, race_name, n):
   fastest_skiers = []
   from operator import itemgetter
   fastest_skiers = sorted(skiers, key=itemgetter('how to get a specific value from a list of tuples'))

Can anyone point me in the right direction?

Thanks!

edit:

The expected results should be something like:

print(fastest_n_times(skiers, 'race_1', 2))
> [skier_2, skier_3]

print(fastest_n_times(skiers,'race_3', 3))
> [skier_3, skier_2, skier_1]

print(fastest_n_times(skiers, 'race_1', 2))
> [skier_2, skier_1]

print(fastest_n_times(skiers, 'race_2', 3))
> [skier_1, skier_2, skier_3]

print(fastest_n_times(skiers, 'race_1', 1))
> [skier_2]

edit 2: I am now trying to create my own function for a key. I can get the race times correct, but I don't know how to use it as a key. I have:

def fastest_n_times(skiers, race_name, n):
    fastest_times = []

    fastest_times = sorted(skiers, key=get_race_key(???, race_name))
    return(fastest_times[:n])

def get_race_key(skier, race_name):
    key_values = []

    key_values = [y for x,y in skier['race_name'] if x == race_name]
    return(key_values[0])

I know normally it would be something like: fastest_times = sorted(skiers, key=get_race_key)

but I need that race_name passed in for the correct time.

For comparing the tuple you have to access times key's values :

    import collections
    new_dict = collections.OrderedDict()


    skier_1 = {'id':123,
               'first_name':'John',
               'last_name':'Smith',
                'times':[('race_1',32.25),('race_2',33.5),('race_3',44)]}

    skier_2 = {'id':234,
               'first_name':'Allison',
               'last_name':'Anderson',
                'times':[('race_1',29.5),('race_2',41),('race_3',40.25)]}

    skier_3 = {'id':456,
               'first_name':'Bob',
               'last_name':'Johnson',
                'times':[('race_1',31),('race_2',41),('race_3',39.75)]}

    skiers = [skier_1, skier_2, skier_3]
    skiers_names=['skier_1','skier_2','skier_3']

    for i,j in zip(skiers,skiers_names):
        new_dict[j]=i.get('times')




    new=[]

    for i in skiers:
        new.append(i.get('times'))

    race_1=[]
    race_2=[]
    race_3=[]

    for i in new:
        race_1.append(i[0])
        race_2.append(i[1])
        race_3.append(i[2])

    race_1.sort(key=lambda tup: tup[1])  # sorts in place
    race_2.sort(key=lambda tup: tup[1])  # sorts in place
    race_3.sort(key=lambda tup: tup[1])  # sorts in place





    def fastest_n_times(skiers, race_name, n):

        return_list=[]

        for i in race_name[:n]:
            for j,k in new_dict.items():
                if i in k:
                    if j not in return_list:
                        return_list.append(j)

        return return_list

output:

>> print(fastest_n_times(skiers, race_1, 2))

['skier_2', 'skier_3']

>> print(fastest_n_times(skiers,race_3, 3))

['skier_3', 'skier_2', 'skier_1']

>> print(fastest_n_times(skiers, race_2, 3))

['skier_1', 'skier_2', 'skier_3']

>> print(fastest_n_times(skiers, race_1, 1))

['skier_2']

To implement your fastest_n_times function, you need first to collect the time of the selected race for each skier, in order to sort by time.

Then, you can extract the n fastest skiers.

For instance:

def fastest_n_times(skiers, selected_race, n):
    time_skier_list = []
    for skier in skiers:
        for race, time in skier['times']:
            if race == selected_race:
                time_skier_list.append((time, skier))
    time_skier_list.sort()
    n_fastest = time_skier_list[:n]
    return [f[1] for f in n_fastest]

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