2021年1月19日星期二

Accessing data in dictionary nested within a list

Having some issues iterating through a dictionary nested in a list. Essentially, I have data from another file that looks like so:

TEAMS = [      'Panthers',      'Bandits',      'Warriors',  ]    PLAYERS = [{          'name': 'Karl Saygan',          'guardians': 'Heather Bledsoe',          'experience': 'YES',          'height': '42 inches'      },      {          'name': 'Matt Gill',          'guardians': 'Charles Gill and Sylvia Gill',          'experience': 'NO',          'height': '40 inches'      },      {   'name': 'Sammy Adams',          'guardians': 'Jeff Adams and Gary Adams',          'experience': 'NO',          'height': '45 inches'      },      {          'name': 'Chloe Alaska',          'guardians': 'David Alaska and Jamie Alaska',          'experience': 'NO',          'height': '47 inches'      },      {          'name': 'Bill Bon',          'guardians': 'Sara Bon and Jenny Bon',          'experience': 'YES',          'height': '43 inches'      },      {          'name': 'Joe Kavalier',          'guardians': 'Sam Kavalier and Elaine Kavalier',          'experience': 'NO',          'height': '39 inches'      },      {          'name': 'Phillip Helm',          'guardians': 'Thomas Helm and Eva Jones',          'experience': 'YES',          'height': '44 inches'      },      {          'name': 'Les Clay',          'guardians': 'Wynonna Brown',          'experience': 'YES',          'height': '42 inches'      }    ,      {          'name': 'Sal Dali',          'guardians': 'Gala Dali',          'experience': 'NO',          'height': '41 inches'      },      {          'name': 'Suzane Greenberg',          'guardians': 'Henrietta Dumas',          'experience': 'YES',          'height': '44 inches'      }  ]  

What I would like to do is create a function called balance_team. What this function would do is is divide the total number of players by the total number of teams and save that as a variable called max_players. Once that's saved, it'll go through the list and append players to the three teams listed below evenly without exceeding the max_number variable. So far I've only got this: None

Here's my code:

          import random  from constants import PLAYERS,TEAMS    players = PLAYERS.copy()  teams = TEAMS.copy()  max_players = len(players)/len(teams)    panthers = []  bandits = []  warriors = []                      def balance_teams(players):      for player in players:          player_name = player['name']                    if len(panthers) < max_players:              panthers.append([player_name])          elif len(bandits) < max_players:              bandits.append([player_name])          elif len(warriors) < max_players:              warriors.append([player_name])    
https://stackoverflow.com/questions/65802113/accessing-data-in-dictionary-nested-within-a-list January 20, 2021 at 10:07AM

没有评论:

发表评论