嵌套字典的Python列表

时间:2014-11-14 02:47:19

标签: python

最初我必须创建一个接收人物属性的函数,并返回一个看起来像这样的结构:

Team:
    Name: Real Madrid
    President:
        Name: Florentino Perez
        Age: 70
        Country: Spain
        Office: 001
    Coach: 
        Name: Carlo Ancelotti
        Age: 55
        Country: Italy
        Office: 006
        Coach License: 456789545678
    Players:
        - Name: Cristiano Ronaldo
          Age: 30
          Country: Portugal
          Number: 7
          Position: Forward
          Golden Balls: 1
        - Name: Chicharito
          Age: 28
          Country: Mexico
          Number: 14
          Position: Forward
        - Name: James Rodriguez
          Age: 22
          Country: Colombia
          Number: 10
          Position: Midfielder
        - Name: Lucas Modric
          Age: 28
          Country: Croatia
          Number: 19
          Position: Midfielder

此结构还包含有关其他俱乐部的信息。我设法使用以下功能执行此操作:

def create_person(name, age, country, **kwargs):
    info={"Name": name, "Age": age, "Country": country}
    for k,v in kwargs.iteritems():
        info[k]=v

    return info

我使用此函数创建嵌套字典列表,并为每个团队显示正确的结构。例如:

teams = [
    {
        "Club Name": "Real Madrid",
        "Club President": create_person("Florentino Perez", 70, "Spain", Office="001"),
        "Club's Coach": create_person("Carlo Angelotii", 60, "Italy", Office="006", CoachLicense="456789545678"),
        "Players": {
            "Real_Player1": create_person("Cristiani Ronaldo", 30, "Portugal", Number="7", Position="Forward", GoldenBalls="1"),
            "Real_Player2": create_person("Chicharito", 28, "Mexic", Number="14", Position="Forward"),
            "Real_Player3": create_person("James Rodriguez", 22, "Columbia", Number="10", Position="Midfilder"),
            "Real_Player4": create_person("Lucas Modric", 28, "Croatia", Number="19", Position="Midfilder")
            }
        },
    {
        "Club Name": "Barcelona",
        "Club President": create_person("Josep Maria Bartolomeu", 60, "Spain", Office="B123"),
        "Club's Coach": create_person("Luis Enrique Martinez", 43, "Spain", Office="B405", CoachLicense="22282321231"),
        "Players": {
            "Barcelona_Player1": create_person("Lionel Messi", 28, "Argentina", Number="10", Position="Forward", GoldenBalls="3"),
            "Barcelona_Player2": create_person("Xavi Hernandez", 34, "Spain", Number="6", Position="Midfilder"),
            "Barcelona_Player3": create_person("Dani Alvez", 28, "Brasil", Number="22", Position="Defender"),
            "Barcelona_Player4": create_person("Gerard Pique", 29, "Spain", Number="22", Position="Defender")
            }
        }
    ]

到目前为止一切都很好。

我遇到的部分是:创建一个接收团队名称的函数print_president打印以下输出:

球队:皇马 主席:弗洛伦蒂诺佩雷斯 年龄:70岁 国家:西班牙 办公室:001

我可以使用变量来显示这个但我需要一个函数而且我不知道如何解决这个问题。请帮忙!

2 个答案:

答案 0 :(得分:1)

当您尝试解决问题(或提出问题)时,请先尽量简化。您的print_president()函数采用团队名称,然后打印有关团队的各种信息。每个团队都是一个具有各种属性的字典。所以问题的简化版本可能如下所示:

teams = [
    {
        'name': 'Real Madrid',
        'pres': 'Florentino',
    },
    {
        'name': 'Barcelona',
        'pres': 'Josep',
    },
]

def print_president(team_name):
    for t in teams:
        # Now, you finish the rest. What should we check here?
        ...

print_president('Barcelona')

答案 1 :(得分:0)

我无法通过团队名称来想办法,因为你必须知道要查看哪个词典。我想是这样的:

def print_president(team):
    print 'Team: {team} President: {president} Age: {age} Country: {country} Office: {office}'.format(
        team=team['Club Name'],
        president=team['Club President']['Name'],
        age=team['Club President']['Age'],
        country=team['Club President']['Country'],
        office=team['Club President']['Office']
    )

如果您正在考虑查看列表中的所有团队,请传递两个参数:teams_list和team_name:

def print_president(teams_list,team_name):
    for team in teams_list:
        if team_name in team.values():
            print 'Team: {team} President: {president} Age: {age} Country: {country} Office: {office}'.format(
                team=team['Club Name'],
                president=team['Club President']['Name'],
                age=team['Club President']['Age'],
                country=team['Club President']['Country'],
                office=team['Club President']['Office']
            )