gpt4 book ai didi

python - 使用列表和字符串方法在 python 中检测单词

转载 作者:太空宇宙 更新时间:2023-11-03 18:52:52 24 4
gpt4 key购买 nike

我正在开发一个程序来检测Python中的单词。用户输入 = 我的电子邮件 ID 是 harry@hogwarts.com输出 = 我的电子邮件 ID 是 xxxxx@hogwarts.com

这就是我目前所拥有的

def main():
message = []
userInput = str(input("Enter the sentence: "))
splitInput = str(list(userInput))
print(splitInput)
for item in splitInput:
indeces = splitInput.index('@')
while((indeces-1).isalpha()):
item = 'x'
message.append(item)
print(' '.join(message))

这是我得到的错误

File "C:\Users\Manmohit\Desktop\purifier.py", line 8, in main
while((indeces-1).isalpha()):
AttributeError: 'int' object has no attribute 'isalpha'

我尝试过在网上寻找不同的方法。我想要类似于 alpha 方法的东西。我应该编写自己的 alpha 方法来检查还是可以使用内置的东西???感谢帮助。谢谢

更新:

将循环 while((indeces-1).isalpha()): 更改为 while((str(indeces-1)).isalpha()): 我没有收到错误,但也没有得到任何输出。

最佳答案

您可以使用此函数对电子邮件进行编码:

>>> def encodeemail(email):
e = email.split("@")
return "@".join(["x" * len(e[0]), e[1]])

>>> encodeemail("harry@hogwarts.com")
xxxxx@hogwarts.com

甚至

>>> def encodeemail(email):
d = email.split(" ")
for i, f in enumerate(d):
e = f.split("@")
if len(e) > 1: d[i] = "@".join(["x" * len(e[0]), e[1]])
return " ".join(d)

>>> encodeemail("this is harry@hogwarts.com")
this is xxxxx@hogwarts.com

不枚举:

>>> def encodeemail(email):
d = email.split(" ")
for i in range(len(d)):
e = d[i].split("@")
if len(e) > 1: d[i] = "@".join(["x" * len(e[0]), e[1]])
return " ".join(d)

关于python - 使用列表和字符串方法在 python 中检测单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17911217/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com