[英]using nested loops - for loop
I have 2 questions regarding using nested loops.我有 2 个关于使用嵌套循环的问题。
colors = []
for color in soccer_match:
colors.append(color['colors'])
colors
Which gives me [['blue', 'white', 'red'], ['green', 'gold']].这给了我 [['blue', 'white', 'red'], ['green', 'gold']]。 How would I be able to combine the two lists into one?
我如何才能将两个列表合并为一个? I haven't learned list comprehension or functions yet.
我还没有学习列表理解或函数。
Here is the given example.这是给定的示例。 Thank you!
谢谢!
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
。
You should name your variables according to what they represent.您应该根据变量所代表的内容命名变量。 In this case, each element of
soccer_match
represents a team .在这种情况下,
soccer_match
每个元素soccer_match
代表一个team 。
Now for each team, team['colors']
is a list.现在对于每个团队,
team['colors']
是一个列表。 For example, the first team has team['colors'] = ["blue", "white", "red"]
.例如,第一支球队有
team['colors'] = ["blue", "white", "red"]
。 Since you want to add all elements of this list to your main colors
list, use the lst.extend(x)
method to add all elements to your current list lst
from a given list x
.由于您想将此列表的所有元素添加到主
colors
列表,请使用lst.extend(x)
方法将给定列表x
所有元素添加到当前列表lst
。 lst.append(x)
adds the entire list x
as a single element to lst
. lst.append(x)
将整个列表x
作为单个元素添加到lst
。 See the documentation查看文档
To find captains, loop over all players in each team.要找到队长,请遍历每支球队的所有球员。 If that player's
'captain'
key is True
, append the player to the captains
list.如果该玩家的
'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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.