首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在for循环中只打印一次

在for循环中只打印一次
EN

Stack Overflow用户
提问于 2016-11-12 21:14:10
回答 2查看 104关注 0票数 0

我有一个使用英雄联盟API的机器人。我只想从一个特定的比赛中检索一些数据。我有代码工作,但它是长和重复的,我想清理它。现在,代码的主要功能正在工作,但在我将其放入bot之前,我正在进行一些测试。现在这是密码,我会解释的。

代码语言:javascript
复制
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:

代码语言:javascript
复制
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)

我想做的是打印“蓝队”和“红队”时,只满足条件一次。这样我就可以在机器人工作的聊天区打印一个干净的页面,但是每次我运行代码时,它都会为每个参赛选手打印“蓝队”或“红队”,比方说:

代码语言:javascript
复制
Blue Team
player 1
Blue Team
player 2...

以此类推,直到总共有10名球员被打印出来。我想做的是:

代码语言:javascript
复制
Blue Team
player1
player2
player3
player4
player5

Red Team
player6
player7
player8
player9
player10

就是这样。(谢谢你的帮助:)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-12 21:28:13

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2016-11-12 21:39:13

我会储存一本字典,然后把所有的玩家打印出来。

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40567720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档