Friday 15 February 2013

Python lists of nested dictionaries -



Python lists of nested dictionaries -

initially had create function receives person's attributes , returns construction looks that:

class="lang-none prettyprint-override">team: name: real madrid president: name: florentino perez age: 70 country: kingdom of 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: forwards golden balls: 1 - name: chicharito age: 28 country: united mexican states number: 14 position: forwards - name: james rodriguez age: 22 country: republic of colombia number: 10 position: midfielder - name: lucas modric age: 28 country: republic of croatia number: 19 position: midfielder

this construction contains info other clubs . managed next function:

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

i used function create list of nested dictionaries , display right construction each team. example:

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") } } ]

everything fine far.

the part got stuck this: create function print_president receives team name prints next output:

team: real madrid president: florentino perez age: 70 country: kingdom of spain office: 001

i utilize variable display need function , don't know how work around this. please help!

when you're trying solve problem (or inquire question) first simplify much can. print_president() function takes team name , prints various pieces of info team. each team dictionary various attributes. simplified version of problem might this:

teams = [ { 'name': 'real madrid', 'pres': 'florentino', }, { 'name': 'barcelona', 'pres': 'josep', }, ] def print_president(team_name): t in teams: # now, finish rest. should check here? ... print_president('barcelona')

python

No comments:

Post a Comment