【问题标题】:python:Move a specific word at the end of a stringpython:在字符串末尾移动一个特定的单词
【发布时间】:2020-11-04 20:25:19
【问题描述】:

我学习了 python,我做了一个不和谐的机器人。在“anivia”之后打印元素有一些困难。我不能说'texte'中是否有“anivia”,我可以数他,但我不知道如何在“anivia”之后打印元素,如果有人可以帮助我请:)

@bot.command()
async def counter(ctx, *champion):
    champion = " ".join(champion)
    url = "https://u.gg/lol/champions/"
    counter = "/counter"
    uurl = url + champion + counter
    await ctx.send(uurl)

    import urllib.request
    with urllib.request.urlopen(uurl) as response:
        texte = response.read()
    if ("anivia" in str(texte)):
        print("Le mot existe !")
    else:
        print("Le mot n'existe pas!")
    test = str(texte)

    z = test.count('anivia')
    print(z)

我可以用 z 数 9 个“anivia”,我想在所有 anivia 之后打印下一个元素(例如:“你好,我是 anivia,我喜欢 anivia 测试”:and, 测试)。

感谢您的帮助:)

【问题讨论】:

    标签: python python-3.x discord


    【解决方案1】:

    这是一种使用辅助变量来标记何时需要打印下一个单词的方法。

    test_string = "Hello, I am anivia on mid or anivia jungle"
    
    do_print = False
    splitted = test_string.split()
    for word in splitted:
        if do_print:
            do_print = False
            print(word)
        if word == "anivia":
            do_print = True
    
    

    输出:

    on
    jungle
    

    【讨论】:

    • 没问题,我建议查看@ricekab 给你的正则表达式答案,记得点赞并接受你认为最有帮助的答案:)
    • AHHHHH 我不明白堆栈溢出WWWWWWWWW
    • 每个答案旁边都有一个勾号,您可以点击它接受答案。此外,每个答案旁边都有一个赞成/反对按钮,点击它会赞成/反对答案
    【解决方案2】:

    如果您熟悉正则表达式(regex),这将变得非常简单:

    import re
    
    # This pattern will capture the first word that comes after "anivia"
    pattern = r'anivia (\w+)'
    
    # Using this string as our example input
    example_string = "anivia first anivia second and finally anivia third"
    
    results = re.findall(pattern, example_string)
    
    print(results)  # Output: ['first', 'second', 'third']
    

    【讨论】:

      【解决方案3】:

      是的,这些解决方案适用于字符串(我也尝试过使用正则表达式)但是

      
          do_print = False
          splitted = test_string.split()
          for word in splitted:
              # print(word)
      
              if do_print:
                  do_print = False
              if word == "anivia":
                  do_print = True
          
          test_string = str(texte)
      
          do_print = False
          splitted = test_string.split()
          for word in splitted:
              # print(word)
      
              if do_print:
                  do_print = False
                  # print(word)
              if word == "champion_id":
                  do_print = True`` 
      
      on the first case i have the ("on" and the "jungle") but with my str(texte), that's doesn't fonction :S.
       If someone knows why, the 2 test_strings are "strings" 
      
       ^^ ty for your answers :)
      

      【讨论】:

      • 您可以在答案下添加 cmets 以要求澄清或是否有问题。这样,您还可以通知该人,以便他们(希望)更快地做出回应。你能解释一下正则表达式解决方案的哪一部分不适合你吗?
      猜你喜欢
      • 2017-04-12
      • 1970-01-01
      • 2019-08-25
      • 2018-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多