2016-11-12 4 views
0

리그 범례 API를 사용하는 봇을 보유하고 있습니다. 난 단지 특정 경기에서 몇 가지 통계를 검색하고 싶습니다. 코드는 작동하지만 길고 반복적이어서 코드를 정리하고 싶었습니다. 지금은 코드의 주요 기능이 작동하지만 내가 봇에 넣기 전에 몇 가지 테스트를하고 있습니다. 지금이 코드입니다, 내가 설명 할게.for 루프에서 무언가를 한 번만 인쇄하십시오.

for i in range(0, 9): 
    num += 1 
    i = r_match['participants'][num] 
    e_name = i['summonerName'] 
    e_id = i['summonerId'] 
    team_id = i['teamId'] 

    r_team = requests.get("https://lan.api.pvp.net/api/lol/lan/v2.5/league/by-summoner/{}/" 
          "entry?api_key=".format(e_id)).json() 

    x = r_team["{}".format(e_id)][0] 
    e_tier = x['tier'] 
    e_div = x['entries'][0]['division'] 

    if team_id == 100: 
     print("Blue team") 
     print(e_name, e_tier, e_div) 

    elif team_id == 200: 
     print("Red team") 
     print(e_name, e_tier, e_div) 

따라서 코드의이 부분은 경기의 참가자의 이름을 가져옵니다, 그것은 자신의 ID를 가져옵니다 및 해당 ID와는 다른 통계를 찾습니다. 두 팀이 있습니다. Json 응답에서 각 팀마다 ID가 있습니다. (100, 200) 여기서 볼 수 있습니다 :

if team_id == 100: 
    print("Blue team") 
    print(e_name, e_tier, e_div) 

elif team_id == 200: 
    print("Red team") 
    print(e_name, e_tier, e_div) 

내가하고 싶은 것은 인쇄 "블루 팀"하고 조건이 충족되는 한 번만 "레드 팀"입니다. 이렇게하면 로봇이 작동하는 채팅에 멋지게 인쇄 할 수 있지만 코드를 실행할 때마다 경기의 각 참가자에게 "파란색 팀"또는 "빨간색 팀"이 인쇄됩니다.

등 총 10 명의 플레이어가 인쇄 될 때까지 계속됩니다. 원하는 작업은 다음과 같습니다.

Blue Team 
player1 
player2 
player3 
player4 
player5 

Red Team 
player6 
player7 
player8 
player9 
player10 

그리고 그게 전부입니다. 어떤 도움 주셔서 감사합니다 :)

답변

0
ids_seen = set() 

for i in range(0, 9): 
    num += 1 
    i = r_match['participants'][num] 
    e_name = i['summonerName'] 
    e_id = i['summonerId'] 
    team_id = i['teamId'] 

    r_team = requests.get("https://lan.api.pvp.net/api/lol/lan/v2.5/league/by-summoner/{}/" 
          "entry?api_key=".format(e_id)).json() 

    x = r_team["{}".format(e_id)][0] 
    e_tier = x['tier'] 
    e_div = x['entries'][0]['division'] 


    if team_id == 100: 
     if not team_id in ids_seen: 
      print("Blue team") 
     print(e_name, e_tier, e_div) 

    elif team_id == 200: 
     if not team_id in ids_seen: 
      print("Red team") 
     print(e_name, e_tier, e_div) 

    ids_seen.add (team_id) 
0

사전을 저장하고 나중에 모든 플레이어를 인쇄합니다.

teams = {} 

for _ in range(9): 
    num += 1 
    i = r_match['participants'][num] 
    e_name = i['summonerName'] 
    e_id = i['summonerId'] 
    team_id = i['teamId'] 

    if team_id not in teams: 
     teams[str(team_id)] = list() 

    r_team = requests.get("https://lan.api.pvp.net/api/lol/lan/v2.5/league/by-summoner/{}/" 
          "entry?api_key=".format(e_id)).json() 

    x = r_team["{}".format(e_id)][0] 
    e_tier = x['tier'] 
    e_div = x['entries'][0]['division'] 

    teams[str(team_id)].append((e_name, e_tier, e_div,)) 

# Outside loop  

print("Blue team") 
for data in teams['100']: 
    print(*data) 

print("Red team") 
for data in teams['200']: 
    print(*data) 
+0

글쎄, 위의 대답은 일을했고 나는 물을 수도 있습니다. 이것은 조금 더 길어 보이므로 이렇게하는 것의 장점은 무엇입니까? – Aguxez

+0

약 4-6 줄만 추가했습니다. 이렇게하면 모든 플레이어가 각 팀 ID에 추가됩니다. 팀 100과 200 이상을 보유하고 있다면, 이는보다 유연합니다 –

관련 문제