[英]python:Move a specific word at the end of a string
i learn python and i do a discord bot.我学习了 python,我做了一个 discord 机器人。 I have some difficulties to print the element after "anivia".
在“anivia”之后打印元素有一些困难。 i cant say if there is "anivia" in the 'texte' and i can count him but i don't know how to print the element after "anivia", if someone can help me please:)
我不能说'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)
I can count 9 "anivia" with z and i want to print the next element after all the anivia (example: " hello im anivia and i like anivia test": and , test ).我可以用 z 数 9 个“anivia”,并且我想在所有 anivia 之后打印下一个元素(例如:“hello im anivia and i like anivia test”: and , test )。
Thanks for your help:)谢谢你的帮助:)
If you're familiar with regular expressions (regex), this becomes very simple:如果您熟悉正则表达式 (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']
Here is an approach that uses an auxiliary variable to mark when the next word needs to be printed.这是一种使用辅助变量来标记何时需要打印下一个单词的方法。
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
Output: Output:
on
jungle
yeah, the those solution works with strings (i tried too with regex) but是的,这些解决方案适用于字符串(我也尝试过使用正则表达式)但是
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 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.