从字典中获取数据的更简单方法
simpler way for getting data from dictionary
我有一本字典:
元组的最后一个条目是射门的分钟数。
例如:第90分钟巴西球员奥斯卡射门
players = {
"Brazil": [
(12, "Júlio César", "Goalkeeper", []),
(4, "David Luiz", "Defender", []),
(6, "Marcelo", "Defender", []),
(13, "Dante", "Defender", []),
(23, "Maicon", "Defender", []),
(5, "Fernandinho", "Midfielder", []),
(7, "Hulk", "Midfielder", []),
(8, "Paulinho", "Midfielder", []),
(11, "Oscar", "Midfielder", [90]),
(16, "Ramires", "Midfielder", []),
(17, "Luiz Gustavo", "Midfielder", []),
(19, "Willian", "Midfielder", []),
(9, "Fred", "Striker", []),
],
"Germany": [
(1, "Manuel Neuer", "Goalkeeper", []),
(4, "Benedikt Höwedes", "Defender", []),
(5, "Mats Hummels", "Defender", []),
(16, "Philipp Lahm", "Defender", []),
(17, "Per Mertesacker", "Defender", []),
(20, "Jérôme Boateng", "Defender", []),
(6, "Sami Khedira", "Midfielder", [29]),
(7, "Bastian Schweinsteiger", "Midfielder", []),
(8, "Mesut Özil", "Midfielder", []),
(13, "Thomas Müller", "Midfielder", [11]),
(14, "Julian Draxler", "Midfielder", []),
(18, "Toni Kroos", "Midfielder", [24, 26]),
(9, "André Schürrle", "Striker", [69, 79]),
(11, "Miroslav Klose", "Striker", [23]),
],
}
我想创建一个函数,以球队和一分钟作为输入,returns那一分钟的进球数。
我这样试过:
def score_at_minute(team,minute):
global players
goals = [goals for (number, player, position, goals) in players[team] if len(goals)>=1]
goals_1 = [goal[0] for goal in goals if len(goal)>=1 if goal[0] <= minute]
goals_2 = [goal[1] for goal in goals if len(goal) ==2 if goal[1]<= minute]
all_goals = goals_1+goals_2
return len(all_goals)
我的函数调用如下所示:
score_at_minute("Germany",90)
是否有更简单的方法或最好的方法是一个班轮?
这个怎么样-
def score_at_minute(x,y):
return len([j for i in players.get(x) for j in i[3] if j<=y])
score_at_minute("Germany",25)
3
另一个版本-
score_at_minute = lambda x,y: len([j for i in players.get(x) for j in i[3] if j<=y])
score_at_minute('Germany', 90)
7
这是如何工作的 -
players.get(x)
获取国家 x 下的球员和进球列表
i[3]
是包含目标列表的项目
- 对此进行迭代时,您将得到一个列表列表,其中每个子列表都包含目标。
[[],[],[],[24],[32,45]]
..等等
- 您现在可以使用
[item for sublist in list for item in sublist]
展平列表列表
- 然后您可以在此列表上应用过滤器(小于等于输入的分钟数)
- 最后计算长度。
您可以简单地使用列表理解
# For Germany
# For less than 90 minutes
len([eg for _,_,_,g in players['Germany'] for eg in g if eg<90])
7
使用items()
def score_at_minute(team,minute):
for key,val in players.items():
if key==team:
return(len([j for i in val for j in i[3] if j <=minute]))
我有一本字典: 元组的最后一个条目是射门的分钟数。 例如:第90分钟巴西球员奥斯卡射门
players = {
"Brazil": [
(12, "Júlio César", "Goalkeeper", []),
(4, "David Luiz", "Defender", []),
(6, "Marcelo", "Defender", []),
(13, "Dante", "Defender", []),
(23, "Maicon", "Defender", []),
(5, "Fernandinho", "Midfielder", []),
(7, "Hulk", "Midfielder", []),
(8, "Paulinho", "Midfielder", []),
(11, "Oscar", "Midfielder", [90]),
(16, "Ramires", "Midfielder", []),
(17, "Luiz Gustavo", "Midfielder", []),
(19, "Willian", "Midfielder", []),
(9, "Fred", "Striker", []),
],
"Germany": [
(1, "Manuel Neuer", "Goalkeeper", []),
(4, "Benedikt Höwedes", "Defender", []),
(5, "Mats Hummels", "Defender", []),
(16, "Philipp Lahm", "Defender", []),
(17, "Per Mertesacker", "Defender", []),
(20, "Jérôme Boateng", "Defender", []),
(6, "Sami Khedira", "Midfielder", [29]),
(7, "Bastian Schweinsteiger", "Midfielder", []),
(8, "Mesut Özil", "Midfielder", []),
(13, "Thomas Müller", "Midfielder", [11]),
(14, "Julian Draxler", "Midfielder", []),
(18, "Toni Kroos", "Midfielder", [24, 26]),
(9, "André Schürrle", "Striker", [69, 79]),
(11, "Miroslav Klose", "Striker", [23]),
],
}
我想创建一个函数,以球队和一分钟作为输入,returns那一分钟的进球数。 我这样试过:
def score_at_minute(team,minute):
global players
goals = [goals for (number, player, position, goals) in players[team] if len(goals)>=1]
goals_1 = [goal[0] for goal in goals if len(goal)>=1 if goal[0] <= minute]
goals_2 = [goal[1] for goal in goals if len(goal) ==2 if goal[1]<= minute]
all_goals = goals_1+goals_2
return len(all_goals)
我的函数调用如下所示:
score_at_minute("Germany",90)
是否有更简单的方法或最好的方法是一个班轮?
这个怎么样-
def score_at_minute(x,y):
return len([j for i in players.get(x) for j in i[3] if j<=y])
score_at_minute("Germany",25)
3
另一个版本-
score_at_minute = lambda x,y: len([j for i in players.get(x) for j in i[3] if j<=y])
score_at_minute('Germany', 90)
7
这是如何工作的 -
players.get(x)
获取国家 x 下的球员和进球列表
i[3]
是包含目标列表的项目- 对此进行迭代时,您将得到一个列表列表,其中每个子列表都包含目标。
[[],[],[],[24],[32,45]]
..等等 - 您现在可以使用
[item for sublist in list for item in sublist]
展平列表列表
- 然后您可以在此列表上应用过滤器(小于等于输入的分钟数)
- 最后计算长度。
您可以简单地使用列表理解
# For Germany
# For less than 90 minutes
len([eg for _,_,_,g in players['Germany'] for eg in g if eg<90])
7
使用items()
def score_at_minute(team,minute):
for key,val in players.items():
if key==team:
return(len([j for i in val for j in i[3] if j <=minute]))