Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

python - Creating new lists from dictionaries nested within list

I was tasked with taking this list (Players):

https://github.com/josephyhu/Basketball-Team-Stats-Tool/blob/master/constants.py

And split it up into even teams, not only by quantity of players but also by a number of experienced players. So all teams had the same number of "non-experienced" players and "Experienced players". I was able to split the teams into equal teams pretty easily, but when it came to ensuring all teams had the same number of experienced players, It lost me. Below was my thought process, I thought I could copy the 'PLAYERS' list break that into an experienced list (exp_players) and non-experienced list (nexp_players), but it's backfiring on me. Even though this is no the most efficient way to do it, but I thought it should work. But there has to be a simpler way to do this and I just can't see it. Here's the error I'm getting for my current code:

Traceback (most recent call last):                                                                                               
  File "/home/treehouse/workspace/123.py", line 25, in <module>                                                                  
    panthers = exp_panthers + nexp_panthers                                                                                      
NameError: name 'exp_panthers' is not defined
import random
from constants import PLAYERS
from constants import TEAMS

GREETING = 'BASKETBALL TEAM STATS TOOL
'

players = PLAYERS.copy()
teams = TEAMS.copy()

print(GREETING.upper())


print('-----MENU-----
')

panthers = exp_panthers + nexp_panthers
bandits = exp_bandits + nexp_bandits
warriors = exp_warriors + nexp_warriors

exp_players = []
nexp_players = []
max_eplayers = len(exp_players)/len(teams)
max_neplayers = len(nexp_players)/len(teams)


exp_panthers=[]
exp_bandits= []
exp_warriors=[]
nexp_panthers=[]
nexp_bandits= []
nexp_warriors=[]

def count_exp(players):
    for player in players:
        if player['experience'] == True:
            exp_players.append(player)
        else:
            nexp_players.append(player)

def balance_team_exp(exp_players):
    for player in exp_players:
        player_name = player['name']

        if len(panthers) < max_players:
            exp_panthers.append(player_name)
        elif len(bandits) < max_players:
            exp_bandits.append(player_name)
        elif len(warriors) < max_players:
            exp_warriors.append(player_name)

def balance_team_nexp(nexp_players):
    for player in nexp_players:
        player_name = player['name']

        if len(panthers) < max_players:
            nexp_panthers.append(player_name)
        elif len(bandits) < max_players:
            nexp_bandits.append(player_name)
        elif len(warriors) < max_players:
            nexp_warriors.append(player_name)  

My expected output would be the three teams (panthers, bandits and warriors) would have equal amount of players and equal players that experience == True.

question from:https://stackoverflow.com/questions/65865740/creating-new-lists-from-dictionaries-nested-within-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As i see it you will have to move those variables:

exp_panthers=[]
exp_bandits= []
exp_warriors=[]

to this line of code:

exp_panthers=[]
exp_bandits= []
exp_warriors=[]
nexp_panthers=[]
nexp_bandits= []
nexp_warriors=[]
panthers = exp_panthers + nexp_panthers
bandits = exp_bandits + nexp_bandits
warriors = exp_warriors + nexp_warriors

By doing this i think that you will not get the error name .... is not defined


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...