【问题标题】:Python - List within a list [closed]Python - 列表中的列表[关闭]
【发布时间】:2018-03-27 19:03:10
【问题描述】:

我正在用 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])

【问题讨论】:

  • 问题/疑问是什么?请花时间阅读How to Ask 以及该页面上的其他链接。
  • 什么有任何帮助吗?您发布的代码似乎解决了您提出的问题。
  • 这里可能适合使用字典。
  • 也许使用Teams.append 而不是Teams.extend,因为extend 会给你一个包含所有团队混合在一起的列表,而append 会给你一个包含每个团队单独列表的列表。
  • 寻求帮助不一定是在 SO 的上下文中提出问题。你有什么问题?这只是让别人为你写代码的一种方式吗?

标签: python list embedding


【解决方案1】:

我强烈建议在这里使用字典。字典允许轻松查找键,并且您可以轻松查看团队的花名册:

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']

【讨论】:

    【解决方案2】:

    注意大写名称是为 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})
    

    【讨论】:

    • @Flyy 请接受有效的答案(灰色 --> 答案旁边的绿色勾号)以表示感谢...谢谢 :)
    【解决方案3】:

    我将假设您需要输入超过 1 个团队。对于此应用程序,您最好使用列表字典:

    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()]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-15
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 2023-03-09
      • 1970-01-01
      相关资源
      最近更新 更多