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

在python中使用列表和字符串方法检测出单词,python,Python,我正在开发一个程序,用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 ite

我正在开发一个程序,用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方法来检查还是可以使用内置的方法??? 谢谢你的帮助。多谢各位

更新:


将循环
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)

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

>>> 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)

如果字符串不包含电子邮件,则可以使用
re
模块

>>> s
'my email id is harry@hogwards.com'
>>> re.sub('(?<=\s)\w+(?=@)', lambda y: 'x'*len(y.group()), s)
'my email id is xxxxx@hogwards.com'
>>s
'我的电子邮件id是harry@hogwards.com'

>>>re.sub(“(?如果字符串不包含电子邮件,则可以使用
re
模块

>>> s
'my email id is harry@hogwards.com'
>>> re.sub('(?<=\s)\w+(?=@)', lambda y: 'x'*len(y.group()), s)
'my email id is xxxxx@hogwards.com'
>>s
'我的电子邮件id是harry@hogwards.com'

>>>re.sub(“(?我假设您想检查字符串的一部分是否通过了isalpha()测试。您的代码不是这样做的

虽然可能有更好的方法来解决您的问题,但您也可以让代码正常工作。希望这是有用的

while((indeces-1).isalpha()):
indes是一个整数,就像-1一样,所以应用isalpha的结果是一个int,正如错误所说的

(str(indeces-1)).isalpha()
这也不起作用。这是从int生成一个字符串,因此str(2)的结果是“2”,这也不是您想要的测试

如果要检查字符,只需索引到字符串中,如下所示:

>>> for i in range(len(s)):
...    if s[i].isalpha():
...         print 'x'
...    else:
...         print s[i]

我假设您想检查字符串的一部分是否通过或未通过isalpha()测试。您的代码不是这样做的

虽然可能有更好的方法来解决您的问题,但您也可以让代码正常工作。希望这是有用的

while((indeces-1).isalpha()):
indes是一个整数,就像-1一样,所以应用isalpha的结果是一个int,正如错误所说的

(str(indeces-1)).isalpha()
这也不起作用。这是从int生成一个字符串,因此str(2)的结果是“2”,这也不是您想要的测试

如果要检查字符,只需索引到字符串中,如下所示:

>>> for i in range(len(s)):
...    if s[i].isalpha():
...         print 'x'
...    else:
...         print s[i]


是霍格沃茨,不是霍格沃兹:)只是一个例子。请原谅拼写。这条信息非常有表现力:整数没有isalpha属性。前面的东西。isalpha是整数。你为什么要调用它呢?我想返回每个字符,直到它到达非alpha字符。我想这会行得通。但是你的字符在splitInput中…这是hogw艺术,而不是霍格沃兹:)只是一个例子。请原谅拼写。这条信息非常有表现力:整数没有isalpha属性。前面的东西。isalpha是一个整数。你为什么要调用它呢?我想返回每个字符,直到它到达一个非alpha字符。我想这会起作用。但是你的字符在splitInput中…那就是y简短而有用。谢谢。问题是它会将之前的所有内容清空,例如,如果我对电子邮件进行编码(这是me@me.com)我相信输出将是'xxxxxxxxxx@me.com'我希望输出在哪里'这是xx@me.com“谢谢你接受,但是看看张阳宇用regexp的回答。这很好。只是一个小问题。有没有一种方法我不能使用enumerate?可以做得不同吗?用我们的enumerate添加了一个版本,非常简短和有用。谢谢你。”问题是它会在它之前把所有的东西都删掉,例如如果我写电子邮件(这是me@me.com)我相信输出将是'xxxxxxxxxx@me.com'我希望输出在哪里'这是xx@me.com“谢谢你接受,但是看看张阳宇用regexp回答的问题。效果很好。只是一个小问题。有没有一种方法我不能使用enumerate?它可以做得不同吗?用我们的enumerate添加了版本是的,不需要
\s
。可以吗?”ebody向我解释上述内容??您可能需要阅读。对于这个正则表达式,它意味着用一些x替换@之前的所有字符。\w意味着a-zA-z0-9。是的,不需要
\s
。有人能向我解释上述内容吗??您可能需要阅读。对于这个正则表达式,它意味着用一些x替换@之前的所有字符。\w意味着a-zA-z0-9。