【发布时间】:2020-07-12 00:15:03
【问题描述】:
编辑:
我正在编辑整个帖子,因为它没有我想的那么清楚。
我有这本字典(字典中的字典列表),我需要按 "Position" 和 "Team" 的值对其进行排序,这意味着我需要从这两个键中获取所有值并将它们打印为我在下面展示。我知道如何获取每个值,但我不知道如何获取它们并对其进行排序。
dic = {"Racing" : [ {"Team" : "Racing",
"Player" : "Player 1",
"Position" : "GK" },
{"Team" : "Racing",
"Player" : "Player 2",
"Position" : "DF"} ],
"Independiente" : [ {"Team" : "Independiente",
"Player" : "Player A",
"Position" : "GK"},
{"Team" : "Independiente",
"Player" : "Player B",
"Position" : "DF"} ]
}
我尝试了这段代码,但我仍然无法让它工作。
{k: v for k, v in sorted(dic.items(), key=lambda item: (item[1][2], item[1][0]))}
我的实际字典比这本大得多,我假装让它变得更大。我需要输出类似于:
DF - Independiente
DF - Racing
GK - Independiente
GK - Racing
在这个例子中,我只包含了字符串,但我还需要按整数值对相似的字典进行排序。
【问题讨论】:
-
我相信有人会给你一个很好的答案,看看做一些递归。 :)
-
当你说按职位和团队时,你想要职位的字典排序吗?
-
我将编辑以澄清我的问题。
-
请也显示您想要的结果。
-
不清楚你想要什么。您是否希望它在
dic本身内部进行排序,或者您想要一个新的字典列表。您正在尝试创建具有多个相同键的字典,这是不可能的。
标签: python list sorting dictionary