Python:按字典中元组列表中的值对字典列表进行排序

时间:2022-09-28 14:44:19

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.

我应该写一个函数来返回n滑雪词典的列表,其中传递的比赛时间最快。如果有一个平局,它应该被滑雪者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.

我对如何使用字典skier_x中列表中元组的值对列表进行排序感到困惑。我的计划是完全对列表进行排序,然后返回最高的n。我可以很轻松地按滑雪者身份排序。

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?

我知道这个循环不是正确的方法。但是,这会返回错误TypeError:'tuple'对象不可调用。我知道元组是不可变的,但我不知道它为什么不可调用?

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:

编辑2:我现在正在尝试为密钥创建自己的函数。我可以让比赛时间正确,但我不知道如何使用它作为关键。我有:

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)

我通常知道它会是这样的:faster_times = sorted(skiiers,key = get_race_key)

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

但是我需要将race_name传入正确的时间。

2 个解决方案

#1


0  

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.

要实现您的faster_n_times函数,首先需要为每个滑雪者收集所选种族的时间,以便按时间排序。

Then, you can extract the n fastest skiers.

然后,您可以提取n个最快的滑雪者。

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]

#2


0  

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']

#1


0  

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.

要实现您的faster_n_times函数,首先需要为每个滑雪者收集所选种族的时间,以便按时间排序。

Then, you can extract the n fastest skiers.

然后,您可以提取n个最快的滑雪者。

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]

#2


0  

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']