循环只运行一次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
正在处理的数据是一个包含其中几个字典的列表。也许这是问题的一部分?循环只运行一次python
这里是什么在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}]\
答
我想你想要的是每队拥有的球员名单,在这种情况下,你可以这样做:
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)
这样做是通过玩家循环,如果玩家应该在A队,将其附加到球队字典中的team A
键中的球员列表。如果该列表尚不存在,则setdefault
调用将创建该列表。
答
这更像是一个评论,我没有特权那样做呢。你的循环似乎很好,你期待的结果是什么?你是否试图保留包含该队所有球员的两份名单的字典?
在这种情况下,您每次运行循环而不是附加到列表时,都会为其分配一个新值。尝试是这样的:
players = [{'name':'Player A','teamID':100}, \
{'name':'Player B','teamID':100}, \
{'name':'Player C','teamID':200}, \
{'name':'Player D','teamID':200}]
teams={'Team A':[],'Team B':[]}
for player in players:
if player['teamID'] == 100:
teams['Team A'].append(player['name'])
else:
teams['Team B'].append(player['name'])
for player in teams['Team A']:
print(player)
print()
for player in teams['Team B']:
print(player)
这将产生输出:
Player A
Player B
Player C
Player D
大卫,给我们的数据,请。 'self.gameData [“参与者”]看起来像什么? –
更多代码请 – MichaelMMeskhi
'data'参数根本没有被使用。我假设你的意思是'self.gameData [“参与者”]'是一个字典列表。在这种情况下,这段代码应该可以正常工作。另外,你只需要在字典中给玩家分配2个键。这意味着如果超过2个玩家,你只需要一个带有2个密钥的字典,每个密钥都会有你的列表中最后一个匹配该密钥条件的玩家。 – bigblind