循环只运行一次python

时间:2016-11-16 02:42:01

标签: python loops for-loop

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

正在处理的数据是一个包含多个字典的列表。也许这个问题分开了?

这里是gameData中的内容["参与者"]

[{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}]\

3 个答案:

答案 0 :(得分:0)

我认为你想要的是每个球队都有一个球员名单,在这种情况下你可以这样做:

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调用尚未存在,则<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/vopressed" /> //light blue <item android:state_selected="true" android:drawable="@drawable/voselected" /> //blue <item android:drawable="@drawable/vo" /> //black </selector> 调用会创建该列表。

答案 1 :(得分:0)

如果您的数据不是问题,请尝试以python生成器表达式的形式执行代码:

https://www.python.org/dev/peps/pep-0289/

答案 2 :(得分:0)

这更像是评论,我没有权利这样做。你的循环似乎很好,你期待的结果是什么?您是否试图保留包含该团队中所有玩家的两个列表的字典?

在这种情况下,每次运行循环时都要为其分配一个新值,而不是附加到列表中。尝试类似:

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