在python中导致不需要的重复列表项的嵌套循环
我的小程序使用了防暴API(游戏),我将玩家分为“盟友队”或“敌人队”。由于数据来自JSON,因此涉及到很多列表和dict,我的问题可能就来自于此,尽管我一直无法找到其中的原因 以下是导致问题的部分:在python中导致不需要的重复列表项的嵌套循环,python,json,api,for-loop,dictionary,Python,Json,Api,For Loop,Dictionary,我的小程序使用了防暴API(游戏),我将玩家分为“盟友队”或“敌人队”。由于数据来自JSON,因此涉及到很多列表和dict,我的问题可能就来自于此,尽管我一直无法找到其中的原因 以下是导致问题的部分: first_game_test = game_list[0] summ_team_ID = first_game_test["teamId"] summoners_in_game = first_game_test["fellowPlayers"] ally_team = [] enemy_tea
first_game_test = game_list[0]
summ_team_ID = first_game_test["teamId"]
summoners_in_game = first_game_test["fellowPlayers"]
ally_team = []
enemy_team = []
for i in range(len(summoners_in_game)):
for name, value in summoners_in_game[i].iteritems():
if summoners_in_game[i]["teamId"] == summ_team_ID:
#if summoners_in_game[i] not in ally_team:
summoner_name = idtosummoner.idToSummonerName(summoners_in_game[i]['summonerId'])
summoner_champ = champion_id.champIdGet(summoners_in_game[i]['championId'])
ally_team.append({summoner_name: summoner_champ})
else:
#if summoners_in_game[i] not in enemy_team:
enemy_team.append(summoners_in_game[i])
已多次检查idtosummoner
和champion\u id
模块;我很肯定,这个问题不是从那里产生的。
如您所见,我使用了一个简单的重复检查修复(注释掉)。但是,它开始干扰进一步的编码:召唤者_name
和召唤者_champ
变量在第3或第4个索引处导致错误(我还没有将行添加到else
,因为我想先解决这个问题)
控制台输出显示以下内容:
PS C:\Users\ptnfolder> python matchhistory.py
Nussen
Nussen
Nussen
kimbb
Traceback (most recent call last):
File "matchhistory.py", line 67, in <module>
matchHistory("thebirdistheword")
File "matchhistory.py", line 39, in matchHistory
print idtosummoner.idToSummonerName(summoners_in_game[i].get('summonerId'))
File "C:\Users\ptnfolder\idtosummoner.py", line 10, in idToSummonerName
champ_name_dict = json_data[str(summID)]
KeyError: '29716673'
PS C:\Users\ptnfolder>python matchhistory.py
努森
努森
努森
金布
回溯(最近一次呼叫最后一次):
文件“matchhistory.py”,第67行,在
比赛历史(“thebirdistheword”)
文件“matchhistory.py”,第39行,在matchhistory中
打印idtosummoner.idToSummonerName(游戏[i]中的召唤师[U]。获取('召唤ID'))
文件“C:\Users\ptnfolder\idtosummoner.py”,第10行,在idToSummonerName中
champ\u name\u dict=json\u data[str(summID)]
键错误:“29716673”
奇怪的是,KeyError
实际上应该解析为'kimbb'
——因为for循环以某种方式将每个条目增加了三倍;它工作一次,然后程序崩溃。您正在列表中循环字典的键和值:
for i in range(len(summoners_in_game)):
for name, value in summoners_in_game[i].iteritems():
因此,对于每个键值对,执行循环体。在循环体中,测试特定的键:
因此,对于字典中的每个键,您都要测试'teamId'
键的值是否匹配summ\u team\u ID
此操作执行的次数与字典中有键的次数相同,但您只想测试一个键
只需删除键值对上的循环:
for i in range(len(summoners_in_game)):
if summoners_in_game[i]["teamId"] == summ_team_ID:
summoner_name = idtosummoner.idToSummonerName(summoners_in_game[i]['summonerId'])
summoner_champ = champion_id.champIdGet(summoners_in_game[i]['championId'])
ally_team.append({summoner_name: summoner_champ})
else:
enemy_team.append(summoners_in_game[i])
您不必使用由range()
生成的索引,只需直接在列表上循环,而不必保持索引:
for team in summoners_in_game:
if team["teamId"] == summ_team_ID:
summoner_name = idtosummoner.idToSummonerName(team['summonerId'])
summoner_champ = champion_id.champIdGet(team['championId'])
ally_team.append({summoner_name: summoner_champ})
else:
enemy_team.append(team)
您正在列表中循环字典的键和值:
for i in range(len(summoners_in_game)):
for name, value in summoners_in_game[i].iteritems():
因此,对于每个键值对,执行循环体。在循环体中,测试特定的键:
因此,对于字典中的每个键,您都要测试'teamId'
键的值是否匹配summ\u team\u ID
此操作执行的次数与字典中有键的次数相同,但您只想测试一个键
只需删除键值对上的循环:
for i in range(len(summoners_in_game)):
if summoners_in_game[i]["teamId"] == summ_team_ID:
summoner_name = idtosummoner.idToSummonerName(summoners_in_game[i]['summonerId'])
summoner_champ = champion_id.champIdGet(summoners_in_game[i]['championId'])
ally_team.append({summoner_name: summoner_champ})
else:
enemy_team.append(summoners_in_game[i])
您不必使用由range()
生成的索引,只需直接在列表上循环,而不必保持索引:
for team in summoners_in_game:
if team["teamId"] == summ_team_ID:
summoner_name = idtosummoner.idToSummonerName(team['summonerId'])
summoner_champ = champion_id.champIdGet(team['championId'])
ally_team.append({summoner_name: summoner_champ})
else:
enemy_team.append(team)
我懂了!我认为有必要在循环中循环以访问嵌套字典。它现在工作得很好,非常感谢!我懂了!我认为有必要在循环中循环以访问嵌套字典。它现在工作得很好,非常感谢!