David Galloway David Galloway - 21 days ago 7
Python Question

loop only running once python

def handleData(self, data):
players = self.gameData["participants"]
teams = {}
for player in players:
if player["teamId"] == 100:
teams["team A"] = player
else:
teams["team B"] = player


the data being handled is a list with several dictionaries within it. Maybe that's apart of the problem?

here's what's in gameData["paticipants"]

[{u'profileIconId': 687, u'championId': 92, u'summonerName': u'digbluefire', u'runes': [{u'count': 9, u'runeId': 5245}, {u'count': 5, u'runeId': 5289}, {u'count': 4, u'runeId': 5301}, {u'count': 9, u'runeId': 5317}, {u'count': 1, u'runeId': 5335}, {u'count': 2, u'runeId': 5337}], u'bot': False, u'masteries': [{u'masteryId': 6114, u'rank': 5}, {u'masteryId': 6121, u'rank': 1}, {u'masteryId': 6131, u'rank': 5}, {u'masteryId': 6142, u'rank': 1}, {u'masteryId': 6312, u'rank': 5}, {u'masteryId': 6321, u'rank': 1}, {u'masteryId': 6331, u'rank': 5}, {u'masteryId': 6343, u'rank': 1}, {u'masteryId': 6351, u'rank': 5}, {u'masteryId': 6362, u'rank': 1}], u'spell2Id': 4, u'teamId': 100, u'spell1Id': 11, u'summonerId': 51678281}, {u'profileIconId': 22, u'championId': 114, u'summonerName': u'fkn lit', u'runes': [{u'count': 9, u'runeId': 5245}, {u'count': 9, u'runeId': 5289}, {u'count': 9, u'runeId': 5317}, {u'count': 3, u'runeId': 5337}], u'bot': False, u'masteries': [{u'masteryId': 6111, u'rank': 5}, {u'masteryId': 6122, u'rank': 1}, {u'masteryId': 6131, u'rank': 5}, {u'masteryId': 6141, u'rank': 1}, {u'masteryId': 6151, u'rank': 5}, {u'masteryId': 6162, u'rank': 1}, {u'masteryId': 6211, u'rank': 5}, {u'masteryId': 6223, u'rank': 1}, {u'masteryId': 6231, u'rank': 5}, {u'masteryId': 6241, u'rank': 1}], u'spell2Id': 4, u'teamId': 100, u'spell1Id': 12, u'summonerId': 73080486}, {u'profileIconId': 1226, u'championId': 4, u'summonerName': u'LeeroyJ123', u'runes': [{u'count': 9, u'runeId': 5273}, {u'count': 5, u'runeId': 5297}, {u'count': 4, u'runeId': 5298}, {u'count': 9, u'runeId': 5317}, {u'count': 2, u'runeId': 5357}, {u'count': 1, u'runeId': 5358}], u'bot': False, u'masteries': [{u'masteryId': 6114, u'rank': 5}, {u'masteryId': 6122, u'rank': 1}, {u'masteryId': 6134, u'rank': 5}, {u'masteryId': 6142, u'rank': 1}, {u'masteryId': 6311, u'rank': 5}, {u'masteryId': 6323, u'rank': 1}, {u'masteryId': 6331, u'rank': 5}, {u'masteryId': 6343, u'rank': 1}, {u'masteryId': 6352, u'rank': 5}, {u'masteryId': 6362, u'rank': 1}], u'spell2Id': 4, u'teamId': 100, u'spell1Id': 12, u'summonerId': 32263666}, {u'profileIconId': 1382, u'championId': 53, u'summonerName': u'DethBeard', u'runes': [{u'count': 9, u'runeId': 5245}, {u'count': 9, u'runeId': 5289}, {u'count': 9, u'runeId': 5315}, {u'count': 1, u'runeId': 5343}, {u'count': 2, u'runeId': 5347}], u'bot': False, u'masteries': [{u'masteryId': 6211, u'rank': 5}, {u'masteryId': 6223, u'rank': 1}, {u'masteryId': 6232, u'rank': 5}, {u'masteryId': 6241, u'rank': 1}, {u'masteryId': 6252, u'rank': 5}, {u'masteryId': 6263, u'rank': 1}, {u'masteryId': 6311, u'rank': 5}, {u'masteryId': 6323, u'rank': 1}, {u'masteryId': 6331, u'rank': 5}, {u'masteryId': 6342, u'rank': 1}], u'spell2Id': 3, u'teamId': 100, u'spell1Id': 4, u'summonerId': 21720577}, {u'profileIconId': 1151, u'championId': 51, u'summonerName': u'lildink', u'runes': [{u'count': 9, u'runeId': 5245}, {u'count': 9, u'runeId': 5289}, {u'count': 9, u'runeId': 5317}, {u'count': 3, u'runeId': 5412}], u'bot': False, u'masteries': [{u'masteryId': 6111, u'rank': 5}, {u'masteryId': 6121, u'rank': 1}, {u'masteryId': 6131, u'rank': 5}, {u'masteryId': 6142, u'rank': 1}, {u'masteryId': 6151, u'rank': 5}, {u'masteryId': 6161, u'rank': 1}, {u'masteryId': 6312, u'rank': 5}, {u'masteryId': 6323, u'rank': 1}, {u'masteryId': 6332, u'rank': 5}, {u'masteryId': 6343, u'rank': 1}], u'spell2Id': 4, u'teamId': 100, u'spell1Id': 7, u'summonerId': 24456467}, {u'profileIconId': 713, u'championId': 32, u'summonerName': u'JHShieldWolf', u'runes': [{u'count': 1, u'runeId': 5119}, {u'count': 9, u'runeId': 5273}, {u'count': 1, u'runeId': 5290}, {u'count': 8, u'runeId': 5297}, {u'count': 9, u'runeId': 5317}, {u'count': 1, u'runeId': 5365}, {u'count': 1, u'runeId': 5409}], u'bot': False, u'masteries': [{u'masteryId': 6212, u'rank': 5}, {u'masteryId': 6223, u'rank': 1}, {u'masteryId': 6232, u'rank': 5}, {u'masteryId': 6242, u'rank': 1}, {u'masteryId': 6251, u'rank': 5}, {u'masteryId': 6262, u'rank': 1}, {u'masteryId': 6311, u'rank': 5}, {u'masteryId': 6321, u'rank': 1}, {u'masteryId': 6332, u'rank': 5}, {u'masteryId': 6341, u'rank': 1}], u'spell2Id': 4, u'teamId': 200, u'spell1Id': 11, u'summonerId': 34293748}, {u'profileIconId': 1389, u'championId': 86, u'summonerName': u'xwarallthetimex', u'runes': [{u'count': 9, u'runeId': 5245}, {u'count': 9, u'runeId': 5289}, {u'count': 9, u'runeId': 5317}, {u'count': 3, u'runeId': 5347}], u'bot': False, u'masteries': [{u'masteryId': 6212, u'rank': 5}, {u'masteryId': 6223, u'rank': 1}, {u'masteryId': 6231, u'rank': 5}, {u'masteryId': 6241, u'rank': 1}, {u'masteryId': 6251, u'rank': 5}, {u'masteryId': 6261, u'rank': 1}, {u'masteryId': 6312, u'rank': 5}, {u'masteryId': 6323, u'rank': 1}, {u'masteryId': 6331, u'rank': 5}, {u'masteryId': 6342, u'rank': 1}], u'spell2Id': 12, u'teamId': 200, u'spell1Id': 4, u'summonerId': 35076058}, {u'profileIconId': 1230, u'championId': 238, u'summonerName': u'sxedoc', u'runes': [{u'count': 9, u'runeId': 5253}, {u'count': 9, u'runeId': 5295}, {u'count': 9, u'runeId': 5305}, {u'count': 3, u'runeId': 5335}], u'bot': False, u'masteries': [{u'masteryId': 6114, u'rank': 5}, {u'masteryId': 6121, u'rank': 1}, {u'masteryId': 6134, u'rank': 5}, {u'masteryId': 6142, u'rank': 1}, {u'masteryId': 6312, u'rank': 5}, {u'masteryId': 6323, u'rank': 1}, {u'masteryId': 6331, u'rank': 5}, {u'masteryId': 6343, u'rank': 1}, {u'masteryId': 6351, u'rank': 5}, {u'masteryId': 6362, u'rank': 1}], u'spell2Id': 14, u'teamId': 200, u'spell1Id': 4, u'summonerId': 49719588}, {u'profileIconId': 909, u'championId': 222, u'summonerName': u'AXL Dunder', u'runes': [{u'count': 9, u'runeId': 5245}, {u'count': 9, u'runeId': 5277}, {u'count': 9, u'runeId': 5305}, {u'count': 3, u'runeId': 5412}], u'bot': False, u'masteries': [{u'masteryId': 6111, u'rank': 5}, {u'masteryId': 6122, u'rank': 1}, {u'masteryId': 6131, u'rank': 5}, {u'masteryId': 6142, u'rank': 1}, {u'masteryId': 6151, u'rank': 5}, {u'masteryId': 6164, u'rank': 1}, {u'masteryId': 6212, u'rank': 5}, {u'masteryId': 6223, u'rank': 1}, {u'masteryId': 6231, u'rank': 5}, {u'masteryId': 6242, u'rank': 1}], u'spell2Id': 4, u'teamId': 200, u'spell1Id': 7, u'summonerId': 51667243}, {u'profileIconId': 685, u'championId': 111, u'summonerName': u'FratDaddyBurrito', u'runes': [{u'count': 9, u'runeId': 5273}, {u'count': 2, u'runeId': 5297}, {u'count': 7, u'runeId': 5298}, {u'count': 2, u'runeId': 5317}, {u'count': 7, u'runeId': 5321}, {u'count': 2, u'runeId': 5357}, {u'count': 1, u'runeId': 5365}], u'bot': False, u'masteries': [{u'masteryId': 6212, u'rank': 5}, {u'masteryId': 6223, u'rank': 1}, {u'masteryId': 6231, u'rank': 3}, {u'masteryId': 6232, u'rank': 2}, {u'masteryId': 6242, u'rank': 1}, {u'masteryId': 6252, u'rank': 5}, {u'masteryId': 6262, u'rank': 1}, {u'masteryId': 6311, u'rank': 5}, {u'masteryId': 6322, u'rank': 1}, {u'masteryId': 6332, u'rank': 5}, {u'masteryId': 6342, u'rank': 1}], u'spell2Id': 3, u'teamId': 200, u'spell1Id': 4, u'summonerId': 51132168}]\

Answer

I think what you want is for each team to have a list of players, in which case you can do this:

def handleData(self, data):
    players = self.gameData["participants"]
    teams = {}
    for player in players:
        if player["teamId"] == 100:
            teams.setdefault("team A", []).append(player)
        else:
            teams.setdefault("team B", []).append(player)

What this does is loop through the players, and if the player should be in team A, appends it to the list of players in the team A key in the teams dictionary. The setdefault call creates that list if it doesn't already exist.