使用嵌套循环 - for 循环
using nested loops - for loop
我有 2 个关于使用嵌套循环的问题。
- 我正在迭代 soccer_match 以创建一个包含每个团队颜色的新列表。到目前为止,我有
colors = []
for color in soccer_match:
colors.append(color['colors'])
colors
这给了我 [['blue'、'white'、'red']、['green'、'gold']]。我怎样才能将两个列表合二为一?我还没有学习列表理解或函数。
- 如何遍历 soccer_match 以创建一个仅包含每支球队队长的列表?我假设我应该使用嵌套的 for 循环来查找 'captain' = True 但不确定如何放置该代码。
这是给定的例子。谢谢!
soccer_match = [
{ "home_team": True,
"away_team": False,
"country": "France",
"num_passes": 484,
"passes_completed": 423,
"fouls_committed": 16,
"colors": ["blue", "white", "red"],
"players": [
{
"name": "Hugo LLORIS",
"captain": True,
"shirt_number": 1,
"position": "Goalie"
},
{
"name": "Benjamin PAVARD",
"captain": False,
"shirt_number": 2,
"position": "Defender"
},
{
"name": "Raphael VARANE",
"captain": False,
"shirt_number": 4,
"position": "Defender"
},
{
"name": "Samuel UMTITI",
"captain": False,
"shirt_number": 5,
"position": "Defender"
},
{
"name": "Paul POGBA",
"captain": False,
"shirt_number": 6,
"position": "Midfield"
},
{
"name": "Antoine GRIEZMANN",
"captain": False,
"shirt_number": 7,
"position": "Forward"
},
{
"name": "Kylian MBAPPE",
"captain": False,
"shirt_number": 10,
"position": "Forward"
},
{
"name": "Ousmane DEMBELE",
"captain": False,
"shirt_number": 11,
"position": "Forward"
},
{
"name": "Corentin TOLISSO",
"captain": False,
"shirt_number": 12,
"position": "Midfield"
},
{
"name": "Ngolo KANTE",
"captain": False,
"shirt_number": 13,
"position": "Midfield"
},
{
"name": "Lucas HERNANDEZ",
"captain": False,
"shirt_number": 21,
"position": "Defender"
}
],
},
{ "home_team": False,
"away_team": True,
"country": "Australia",
"num_passes": 390,
"passes_completed": 332,
"fouls_committed": 19,
"colors": ["green", "gold"],
"players": [
{
"name": "Mathew RYAN",
"captain": False,
"shirt_number": 1,
"position": "Goalie"
},
{
"name": "Mark MILLIGAN",
"captain": False,
"shirt_number": 5,
"position": "Defender"
},
{
"name": "Mathew LECKIE",
"captain": False,
"shirt_number": 7,
"position": "Forward"
},
{
"name": "Robbie KRUSE",
"captain": False,
"shirt_number": 10,
"position": "Forward"
},
{
"name": "Andrew NABBOUT",
"captain": False,
"shirt_number": 11,
"position": "Forward"
},
{
"name": "Aaron MOOY",
"captain": False,
"shirt_number": 13,
"position": "Midfield"
},
{
"name": "Mile JEDINAK",
"captain": True,
"shirt_number": 15,
"position": "Midfield"
},
{
"name": "Aziz BEHICH",
"captain": False,
"shirt_number": 16,
"position": "Defender"
},
{
"name": "Joshua RISDON",
"captain": False,
"shirt_number": 19,
"position": "Defender"
},
{
"name": "Trent SAINSBURY",
"captain": False,
"shirt_number": 20,
"position": "Defender"
},
{
"name": "Tom ROGIC",
"captain": False,
"shirt_number": 23,
"position": "Midfield"
}
]
}
]
将 colors.append
替换为 colors.extend
。
您应该根据变量所代表的含义来命名您的变量。在这种情况下,soccer_match
的每个元素代表一个 team.
现在对于每个团队,team['colors']
是一个列表。例如,第一队有 team['colors'] = ["blue", "white", "red"]
。由于您想将此列表的所有元素添加到主 colors
列表,请使用 lst.extend(x)
方法将给定列表 x
中的所有元素添加到当前列表 lst
. lst.append(x)
将整个列表 x
作为单个元素添加到 lst
。请参阅 documentation
要找到队长,请遍历每支球队的所有球员。如果该玩家的 'captain'
键是 True
,则将该玩家附加到 captains
列表。
colors = []
captains = []
for team in soccer_match:
colors.extend(team['colors'])
for player in team['players']:
if player['captain']:
captains.append(player)
我有 2 个关于使用嵌套循环的问题。
- 我正在迭代 soccer_match 以创建一个包含每个团队颜色的新列表。到目前为止,我有
colors = []
for color in soccer_match:
colors.append(color['colors'])
colors
这给了我 [['blue'、'white'、'red']、['green'、'gold']]。我怎样才能将两个列表合二为一?我还没有学习列表理解或函数。
- 如何遍历 soccer_match 以创建一个仅包含每支球队队长的列表?我假设我应该使用嵌套的 for 循环来查找 'captain' = True 但不确定如何放置该代码。
这是给定的例子。谢谢!
soccer_match = [
{ "home_team": True,
"away_team": False,
"country": "France",
"num_passes": 484,
"passes_completed": 423,
"fouls_committed": 16,
"colors": ["blue", "white", "red"],
"players": [
{
"name": "Hugo LLORIS",
"captain": True,
"shirt_number": 1,
"position": "Goalie"
},
{
"name": "Benjamin PAVARD",
"captain": False,
"shirt_number": 2,
"position": "Defender"
},
{
"name": "Raphael VARANE",
"captain": False,
"shirt_number": 4,
"position": "Defender"
},
{
"name": "Samuel UMTITI",
"captain": False,
"shirt_number": 5,
"position": "Defender"
},
{
"name": "Paul POGBA",
"captain": False,
"shirt_number": 6,
"position": "Midfield"
},
{
"name": "Antoine GRIEZMANN",
"captain": False,
"shirt_number": 7,
"position": "Forward"
},
{
"name": "Kylian MBAPPE",
"captain": False,
"shirt_number": 10,
"position": "Forward"
},
{
"name": "Ousmane DEMBELE",
"captain": False,
"shirt_number": 11,
"position": "Forward"
},
{
"name": "Corentin TOLISSO",
"captain": False,
"shirt_number": 12,
"position": "Midfield"
},
{
"name": "Ngolo KANTE",
"captain": False,
"shirt_number": 13,
"position": "Midfield"
},
{
"name": "Lucas HERNANDEZ",
"captain": False,
"shirt_number": 21,
"position": "Defender"
}
],
},
{ "home_team": False,
"away_team": True,
"country": "Australia",
"num_passes": 390,
"passes_completed": 332,
"fouls_committed": 19,
"colors": ["green", "gold"],
"players": [
{
"name": "Mathew RYAN",
"captain": False,
"shirt_number": 1,
"position": "Goalie"
},
{
"name": "Mark MILLIGAN",
"captain": False,
"shirt_number": 5,
"position": "Defender"
},
{
"name": "Mathew LECKIE",
"captain": False,
"shirt_number": 7,
"position": "Forward"
},
{
"name": "Robbie KRUSE",
"captain": False,
"shirt_number": 10,
"position": "Forward"
},
{
"name": "Andrew NABBOUT",
"captain": False,
"shirt_number": 11,
"position": "Forward"
},
{
"name": "Aaron MOOY",
"captain": False,
"shirt_number": 13,
"position": "Midfield"
},
{
"name": "Mile JEDINAK",
"captain": True,
"shirt_number": 15,
"position": "Midfield"
},
{
"name": "Aziz BEHICH",
"captain": False,
"shirt_number": 16,
"position": "Defender"
},
{
"name": "Joshua RISDON",
"captain": False,
"shirt_number": 19,
"position": "Defender"
},
{
"name": "Trent SAINSBURY",
"captain": False,
"shirt_number": 20,
"position": "Defender"
},
{
"name": "Tom ROGIC",
"captain": False,
"shirt_number": 23,
"position": "Midfield"
}
]
}
]
将 colors.append
替换为 colors.extend
。
您应该根据变量所代表的含义来命名您的变量。在这种情况下,soccer_match
的每个元素代表一个 team.
现在对于每个团队,team['colors']
是一个列表。例如,第一队有 team['colors'] = ["blue", "white", "red"]
。由于您想将此列表的所有元素添加到主 colors
列表,请使用 lst.extend(x)
方法将给定列表 x
中的所有元素添加到当前列表 lst
. lst.append(x)
将整个列表 x
作为单个元素添加到 lst
。请参阅 documentation
要找到队长,请遍历每支球队的所有球员。如果该玩家的 'captain'
键是 True
,则将该玩家附加到 captains
列表。
colors = []
captains = []
for team in soccer_match:
colors.extend(team['colors'])
for player in team['players']:
if player['captain']:
captains.append(player)