编辑:
我正在编辑整篇文章,因为它没有我想象的那么清晰
我有这个字典(字典中的字典列表),我需要按"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的值提取为
(Position, Team)
,的元组 连接它们以创建
(Position, Team)
元素的扁平元组。用已排序的
相关问题 更多 >
编程相关推荐