[英]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. 我应该写一个函数来返回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. 我对如何根据字典skier_x中列表中的元组的值对列表进行排序感到困惑。 My plan was to sort the list completely then just return the n highest.
我的计划是将列表完全排序,然后只返回n个最高位。 I can sort by skier id pretty easily.
我可以很容易地按滑雪者ID排序。
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
. 但是,这将返回错误
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. 编辑2:我现在正在尝试为密钥创建自己的函数。 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) 我知道通常是这样的:fastest_times = sorted(滑雪者,钥匙= get_race_key)
but I need that race_name passed in for the correct time. 但我需要在正确的时间内传递race_name。
For comparing the tuple you have to access times key's values : 为了比较元组,您必须访问times键的值:
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. 要实现您的fastly_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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.