Python - 列表中的列表
Python - List within a list
我正在 python 中创建一个程序,我需要向用户询问团队名称和团队成员。我需要在列表中列出团队名称,并将团队成员嵌入到团队名称中。有帮助吗?
# Creating Teams
print("Welcome to the program")
print("======================================================")
Teams = []
# Looping to find out the four different team names and players.
for x in range(0, 4) :
# Asking for the Team name and the team players names
TeamName = input("What is the name of the team?")
Player1 = input("What is the first name of the player in your team?")
Player2 = input("What is the second name of the player in your team?")
Player3 = input("What is the third name of the player in your team?")
Player4 = input("What is the last name of the player in your team?")
Teams.extend([TeamName, Player1, Player2, Player3, Player4])
TeamPlayers = int(input("What Team do you want to see? (0-4)"))
print([Teams, Player1, Player2, Player3, Player4])
我强烈建议在这里使用字典。字典允许轻松查找键,您可以轻松查看您的团队的花名册:
print("Welcome to the program")
print("======================================================")
teams = {}
# Looping to find out the four different team names and players.
for x in range(0, 2) :
# Asking for the Team name and the team players names
name = input("What is the name of the team?: ")
teams[name] = {}
teams[name]['Player1'] = input("What is the first name of the player in your team?: ")
teams[name]['Player2'] = input("What is the second name of the player in your team?: ")
teams[name]['Player3'] = input("What is the third name of the player in your team?: ")
teams[name]['Player4'] = input("What is the last name of the player in your team?: ")
print(teams)
输出:
Welcome to the program
======================================================
What is the name of the team?: Team1
What is the first name of the player in your team?: Chris
What is the second name of the player in your team?: Chris
What is the third name of the player in your team?: Chris
What is the last name of the player in your team?: Chris
What is the name of the team?: Team2
What is the first name of the player in your team?: Chris
What is the second name of the player in your team?: Chris
What is the third name of the player in your team?: Chris
What is the last name of the player in your team?: Chris
{'Team1': {'Player1': 'Chris',
'Player2': 'Chris',
'Player3': 'Chris',
'Player4': 'Chris'},
'Team2': {'Player1': 'Chris',
'Player2': 'Chris',
'Player3': 'Chris',
'Player4': 'Chris'}}
如果您想查看 Team1
上的所有玩家,您只需使用 teams['Team1']
(注意 类 在 Python 中保留大写名称)
字典的字典在这里作为数据结构更有意义,而不是创建大量变量只是为了将它们扔到列表中,列表丢失了它们的键并且只会在生产线上创建更多工作(解包等)
所以,要做到这一点,您可以这样做:
teams = {}
for x in range(0, 4) :
teamName = input("What is the name of the team?")
player1 = input("What is the first name of the player in your team?")
player2 = input("What is the second name of the player in your team?")
player3 = input("What is the third name of the player in your team?")
player4 = input("What is the last name of the player in your team?")
teams[teamName] = {'player_1': player1,
'player_2': player2,
'player_3': player3,
'player_4': player4}
给出了 teams
作为可读和可访问的结构:
{'team one': {'player_4': 'james', 'player_3': 'jim', 'player_1': 'bob', 'player_2': 'bill'},
'team two': {'player_4': 'cat', 'player_3': 'fish', 'player_1': 'jake', 'player_2': 'paul'},
'team three': {'player_4': 'sharpener', 'player_3': 'ruler', 'player_1': 'table', 'player_2': 'paper'},
'team four': {'player_4': 'pencil', 'player_3': 'pen', 'player_1': 'shoe', 'player_2': 'book'}
}
所以,现在,您可以通过以下方式访问:teams['team one']['player_1']
。
或者您可以使用相同的想法,但使用字典列表(并在每个团队的字典中包含团队名称作为 val)。
teams = []
for x in range(0, 4) :
teamName = input("What is the name of the team?")
player1 = input("What is the first name of the player in your team?")
player2 = input("What is the second name of the player in your team?")
player3 = input("What is the third name of the player in your team?")
player4 = input("What is the last name of the player in your team?")
teams.append({'player_1': player1,
'player_2': player2,
'player_3': player3,
'player_4': player4})
我暂时假设您需要输入多个团队。对于此应用程序,您最好使用列表字典:
Teams = {}
players = 2
no_of_teams = 2
for n in range(no_of_teams):
TeamName = input("What is the name of the team?")
Teams[TeamName] = []
for x in range(players):
# Asking for the Team name and the team players names
player = input("Please enter the name of player #%s in your team:" % (x+1))
Teams[TeamName].append(player)
[print(team, players) for team, players in Teams.items()]
或者字典的字典,如果你关心为每个名字分配一个球员号码:
Teams = {}
players = 2
no_of_teams = 2
for n in range(no_of_teams):
TeamName = input("What is the name of the team?")
Teams[TeamName] = {}
for x in range(players):
player = input("Please enter the name of player #%s in your team:" % (x+1))
Teams[TeamName]['player%s' % (x+1)] = player
[print(team, players) for team, players in Teams.items()]
我正在 python 中创建一个程序,我需要向用户询问团队名称和团队成员。我需要在列表中列出团队名称,并将团队成员嵌入到团队名称中。有帮助吗?
# Creating Teams
print("Welcome to the program")
print("======================================================")
Teams = []
# Looping to find out the four different team names and players.
for x in range(0, 4) :
# Asking for the Team name and the team players names
TeamName = input("What is the name of the team?")
Player1 = input("What is the first name of the player in your team?")
Player2 = input("What is the second name of the player in your team?")
Player3 = input("What is the third name of the player in your team?")
Player4 = input("What is the last name of the player in your team?")
Teams.extend([TeamName, Player1, Player2, Player3, Player4])
TeamPlayers = int(input("What Team do you want to see? (0-4)"))
print([Teams, Player1, Player2, Player3, Player4])
我强烈建议在这里使用字典。字典允许轻松查找键,您可以轻松查看您的团队的花名册:
print("Welcome to the program")
print("======================================================")
teams = {}
# Looping to find out the four different team names and players.
for x in range(0, 2) :
# Asking for the Team name and the team players names
name = input("What is the name of the team?: ")
teams[name] = {}
teams[name]['Player1'] = input("What is the first name of the player in your team?: ")
teams[name]['Player2'] = input("What is the second name of the player in your team?: ")
teams[name]['Player3'] = input("What is the third name of the player in your team?: ")
teams[name]['Player4'] = input("What is the last name of the player in your team?: ")
print(teams)
输出:
Welcome to the program
======================================================
What is the name of the team?: Team1
What is the first name of the player in your team?: Chris
What is the second name of the player in your team?: Chris
What is the third name of the player in your team?: Chris
What is the last name of the player in your team?: Chris
What is the name of the team?: Team2
What is the first name of the player in your team?: Chris
What is the second name of the player in your team?: Chris
What is the third name of the player in your team?: Chris
What is the last name of the player in your team?: Chris
{'Team1': {'Player1': 'Chris',
'Player2': 'Chris',
'Player3': 'Chris',
'Player4': 'Chris'},
'Team2': {'Player1': 'Chris',
'Player2': 'Chris',
'Player3': 'Chris',
'Player4': 'Chris'}}
如果您想查看 Team1
上的所有玩家,您只需使用 teams['Team1']
(注意 类 在 Python 中保留大写名称)
字典的字典在这里作为数据结构更有意义,而不是创建大量变量只是为了将它们扔到列表中,列表丢失了它们的键并且只会在生产线上创建更多工作(解包等)
所以,要做到这一点,您可以这样做:
teams = {}
for x in range(0, 4) :
teamName = input("What is the name of the team?")
player1 = input("What is the first name of the player in your team?")
player2 = input("What is the second name of the player in your team?")
player3 = input("What is the third name of the player in your team?")
player4 = input("What is the last name of the player in your team?")
teams[teamName] = {'player_1': player1,
'player_2': player2,
'player_3': player3,
'player_4': player4}
给出了 teams
作为可读和可访问的结构:
{'team one': {'player_4': 'james', 'player_3': 'jim', 'player_1': 'bob', 'player_2': 'bill'},
'team two': {'player_4': 'cat', 'player_3': 'fish', 'player_1': 'jake', 'player_2': 'paul'},
'team three': {'player_4': 'sharpener', 'player_3': 'ruler', 'player_1': 'table', 'player_2': 'paper'},
'team four': {'player_4': 'pencil', 'player_3': 'pen', 'player_1': 'shoe', 'player_2': 'book'}
}
所以,现在,您可以通过以下方式访问:teams['team one']['player_1']
。
或者您可以使用相同的想法,但使用字典列表(并在每个团队的字典中包含团队名称作为 val)。
teams = []
for x in range(0, 4) :
teamName = input("What is the name of the team?")
player1 = input("What is the first name of the player in your team?")
player2 = input("What is the second name of the player in your team?")
player3 = input("What is the third name of the player in your team?")
player4 = input("What is the last name of the player in your team?")
teams.append({'player_1': player1,
'player_2': player2,
'player_3': player3,
'player_4': player4})
我暂时假设您需要输入多个团队。对于此应用程序,您最好使用列表字典:
Teams = {}
players = 2
no_of_teams = 2
for n in range(no_of_teams):
TeamName = input("What is the name of the team?")
Teams[TeamName] = []
for x in range(players):
# Asking for the Team name and the team players names
player = input("Please enter the name of player #%s in your team:" % (x+1))
Teams[TeamName].append(player)
[print(team, players) for team, players in Teams.items()]
或者字典的字典,如果你关心为每个名字分配一个球员号码:
Teams = {}
players = 2
no_of_teams = 2
for n in range(no_of_teams):
TeamName = input("What is the name of the team?")
Teams[TeamName] = {}
for x in range(players):
player = input("Please enter the name of player #%s in your team:" % (x+1))
Teams[TeamName]['player%s' % (x+1)] = player
[print(team, players) for team, players in Teams.items()]