对字典中的字典列表进行排序

2024-09-26 22:13:22 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑:
我正在编辑整篇文章,因为它没有我想象的那么清晰

我有这个字典(字典中的字典列表),我需要按"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

在这个示例中,我只包含字符串,但我还需要按整数值对类似的字典进行排序


Tags: 编辑df列表字典排序positionitemteam
3条回答

我不知道你所说的“按职位和团队排序”是什么意思,也不知道你为输出写了什么

如果您希望输出为

DF - Independiente
DF - Racing
GK - Independiente
GK - Racing  

然后你想要这个,不管你想怎么储存。也许是在一份清单上或是在一份新的口述中

for i in dic.values():
    print(list(i[1].values())[2]+' - '+list(i[1].values())[0])
    print(list(i[1].values())[2]+' - '+list(i[0].values())[0])

我找到的一个解决办法是使用一个辅助命令,在这个命令中,你可以将团队保存在适当的位置。然后在输出数据时按团队和位置进行排序。例如:

from collections import defaultdict

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"} ]
       }
       
dict = defaultdict(list)
for team in dic:
    for player in dic[team]:
        dict[player["Position"]].append(player["Team"])
for position in sorted(dict.keys()):
    for item in sorted(dict[position]):
        print(f"{position} - {item}")

输出:

DF - Independiente
DF - Racing
GK - Independiente
GK - Racing

将dic的值提取为(Position, Team)
的元组 连接它们以创建(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"} ]
       }

res = ()
#           emit as tuple of (Position, Team) from all of dic's values
for PT in ( tuple((team['Position'], team['Team']) for team in v) for v in dic.values() ):
    res = res + PT
    # concatenate all tuples to flatten into one large tuple
res = sorted(res)
# sort tuple of (Position, Team) values

print(res)

# create string representation in format "Position - Team\n..."
print("\n".join(" - ".join(r) for r in res))

相关问题 更多 >

    热门问题