def players_by_country_and_position(squads_list): players = players_by_position(squads_list) result = { 'Argentina': { 'GK': [], 'FW': [] }, 'Belgium': { 'GK': [], 'MF': [] }, 'Brazil': { 'MF': [] }, 'South Korea': { 'FW': [] } } for position in players: for player in players[position]: result[player['country']][player['position']].append(player) return result
def players_by_country_and_position(squads_list): import itertools new_dict = {} for key, group in itertools.groupby(squads_list, key = lambda item: item[6]): new_dict[key] = players_by_position(group) return new_dict
def players_by_country_and_position(squads_list): players = players_by_position(squads_list) country_position_dict = {} for players_list in players.values(): for player in players_list: country_position_dict.setdefault(player["country"], {}) country_position_dict[player["country"]].setdefault(player["position"], []) country_position_dict[player["country"]][player["position"]].append(player) return country_position_dict
def players_by_country_and_position(squads_list): result = {} for position, players in players_by_position(SQUADS_DATA).items(): for player in players: country = player['country'] position = player['position'] result.setdefault(country, {}) result[country].setdefault(position, []) result[country][position].append(player) return result
def players_by_country_and_position(squads_list): positions = players_by_position(squads_list) a_dict = {} for key, value in positions.items(): player1 = len(value) for player in range(player1): country = value[player]['country'] a_dict.setdefault(country, {}) a_dict[country].setdefault(key, []) a_dict[country][key].append(positions[key][player]) return a_dict
def players_by_country_and_position(squads_list): pais_players = {} posiciones_dic = {} pos_players = players_by_position(squads_list) for pos,players in pos_players.items(): for player in players: pais = player["country"] pais_players.setdefault(pais,{}) for key_pais, dic_pos in pais_players.items(): if pais == key_pais: pais_players[pais].setdefault(pos,[]) pais_players[pais][pos].append(player) return pais_players
def players_by_country_and_position(squads_list): player_dictionaries = players_as_dictionaries(squads_list) players_sorted_by_position = players_by_position(squads_list) result = {} for player_as_a_dict in player_dictionaries: country = player_as_a_dict['country'] position = player_as_a_dict['position'] result.setdefault(country, {}) result[country].setdefault(position, []) result[country][position].append(player_as_a_dict) return result
def test_assignment_2(): result = players_by_position(SQUADS_DATA) assert len(result) == 3 # 3 positions goalkeepers = result['GK'] assert len(goalkeepers) == 2 assert goalkeepers[0] == { 'caps': '', 'club': 'Quilmes', 'club_country': 'Argentina', 'country': 'Argentina', 'date_of_birth': '(1908-10-23)23 October 1908 (aged 21)', 'name': 'Juan Botasso', 'number': '1', 'position': 'GK', 'year': '1930' } midfielders = result['MF'] assert len(midfielders) == 8 assert midfielders[0] == { 'caps': '42', 'club': 'Royal Beerschot AC', 'club_country': 'Belgium', 'country': 'Belgium', 'date_of_birth': '(1900-10-26)26 October 1900 (aged 29)', 'name': 'Pierre Braine', 'number': '-', 'position': 'MF', 'year': '1930' } forwards = result['FW'] assert len(forwards) == 4 assert forwards[0] == { 'caps': '', 'club': 'Boca Juniors', 'club_country': 'Argentina', 'country': 'Argentina', 'date_of_birth': '(1907-02-23)23 February 1907 (aged 23)', 'name': 'Roberto Cherro', 'number': '9', 'position': 'FW', 'year': '1930' }