Python 如何将非字母或数字的字符串中的字符发送到字符串末尾?

Python 如何将非字母或数字的字符串中的字符发送到字符串末尾?,python,Python,我正在做一个Pig拉丁代码,其中以下单词应该返回以下响应: "computer" == "omputercay" "think" == "inkthay" "algorithm" == "algorithmway" "office" == "officeway" "Computer" == "Omputerc

我正在做一个Pig拉丁代码,其中以下单词应该返回以下响应:

"computer" == "omputercay"
"think" == "inkthay"
"algorithm" == "algorithmway"
"office" == "officeway"
"Computer" == "Omputercay"
"Science!" == "Iencescay!"
但是,最后一句话,我的代码没有推送“!”到字符串的末尾。什么样的代码可以实现这一点

除了最后一个返回“encience!Scay!”的单词外,它们都返回了正确的单词

def pigLatin(单词):
元音=(“a”、“e”、“i”、“o”、“u”)
第一个字母=单词[0]
如果元音中的第一个字母:
返回单词+way'
其他:
l=len(字)
i=0
而我
为了简单起见,您可以检查一下这个单词是否包含一个发音点
在最后,如果它确实删除了它,当你完成添加回来。因此,与其返回,只需检查位置在末尾(如果您在最后发现它)


为了简单起见,您可以检查一下这个词是否包含一个表达点
在最后,如果它确实删除了它,当你完成添加回来。因此,与其返回,只需检查位置在末尾(如果您在最后发现它)


正则表达式起到了拯救作用。在这种情况下,带有单词边界的正则表达式模式将使您的生活更加轻松。单词边界就是它听起来的样子-它表示单词的开始或结束,并在模式中用
\b
表示。在您的情况下,
就是这样一个词的边界。“单词”本身由集合
a-z
a-z
0-9
或下划线中的任何字符组成,在模式中由
\w
表示。
+
表示一个或多个
\w
字符

因此,如果模式是
r“\b\w+\b”
,这将匹配任何单词(由
a-zA-Z0-9
中的任何一个组成),并具有前导或后继单词边界

import re

pattern = r"\b\w+\b"
sentence = "computer think algorithm office Computer Science!"

print(re.findall(pattern, sentence))
输出:

['computer', 'think', 'algorithm', 'office', 'Computer', 'Science']
>>> 
******** ***** ********* ****** ******** *******!
>>> 
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
>>> 
nptDPbHvsxKNWdYyrTqVQRlBCZShzgGjfkJMLmFXwc
>>> 
Group one is "c"
Group two is "omputer"
>>> 
omputercay inkthay algorithmway officeway Omputercay Iencescay!
>>> 
在这里,我们使用
re.findall
来获得匹配该模式的所有子字符串的列表。请注意,不包括空格或标点符号


让我们介绍一下
re.sub
,它需要查找一个模式、一个字符串和另一个字符串来替换它找到的任何匹配项。您可以传递函数,而不是替换字符串。此函数必须将匹配对象作为参数,并且必须返回一个字符串以替换当前匹配

import re

pattern = r"\b\w+\b"
sentence = "computer think algorithm office Computer Science!"

def replace(match):
    return "*" * len(match.group())

print(re.sub(pattern, replace, sentence))
输出:

['computer', 'think', 'algorithm', 'office', 'Computer', 'Science']
>>> 
******** ***** ********* ****** ******** *******!
>>> 
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
>>> 
nptDPbHvsxKNWdYyrTqVQRlBCZShzgGjfkJMLmFXwc
>>> 
Group one is "c"
Group two is "omputer"
>>> 
omputercay inkthay algorithmway officeway Omputercay Iencescay!
>>> 
那只是为了演示的目的


让我们换一下档位:

from string import ascii_letters as alphabet
print(alphabet)
输出:

['computer', 'think', 'algorithm', 'office', 'Computer', 'Science']
>>> 
******** ***** ********* ****** ******** *******!
>>> 
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
>>> 
nptDPbHvsxKNWdYyrTqVQRlBCZShzgGjfkJMLmFXwc
>>> 
Group one is "c"
Group two is "omputer"
>>> 
omputercay inkthay algorithmway officeway Omputercay Iencescay!
>>> 
这对于创建只包含辅音的字符串非常方便:

from string import ascii_letters as alphabet

consonants = "".join(set(alphabet) ^ set("aeiouAEIOU"))
print(consonants)
输出:

['computer', 'think', 'algorithm', 'office', 'Computer', 'Science']
>>> 
******** ***** ********* ****** ******** *******!
>>> 
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
>>> 
nptDPbHvsxKNWdYyrTqVQRlBCZShzgGjfkJMLmFXwc
>>> 
Group one is "c"
Group two is "omputer"
>>> 
omputercay inkthay algorithmway officeway Omputercay Iencescay!
>>> 
我们已经考虑了所有字母字符集和仅元音字符集之间的差异。这只产生一组辅音。请注意,它没有保留在一个集合中的字符顺序,但在我们的例子中并不重要,因为我们将有效地将此字符串视为一个集合-测试成员资格(如果此字符串中有一个字符,它必须是辅音。顺序不重要)


让我们利用这一点,修改前面的模式。让我们添加两个捕获组-第一个将捕获任何前导辅音(如果存在),第二个将捕获终止单词边界之前的所有剩余字母字符(辅音或元音):

import re
from string import ascii_letters as alphabet
consonants = "".join(set(alphabet) ^ set("aeiouAEIOU"))

pattern = fr"\b([{consonants}]*)(\w+)\b"
word = "computer"

match = re.match(pattern, word)
if match is not None:
    print(f"Group one is \"{match.group(1)}\"")
    print(f"Group two is \"{match.group(2)}\"")
输出:

['computer', 'think', 'algorithm', 'office', 'Computer', 'Science']
>>> 
******** ***** ********* ****** ******** *******!
>>> 
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
>>> 
nptDPbHvsxKNWdYyrTqVQRlBCZShzgGjfkJMLmFXwc
>>> 
Group one is "c"
Group two is "omputer"
>>> 
omputercay inkthay algorithmway officeway Omputercay Iencescay!
>>> 
如您所见,第一组捕获了
c
,第二组捕获了
computer
。在以后构建pig拉丁翻译时,将匹配分为两组将非常有用。通过命名我们的捕获群,我们可以变得更加可爱。这不是必需的,但它会使以后的阅读变得更容易:

pattern = fr"\b(?P<prefix>[{consonants}]*)(?P<rest>\w+)\b"
输出:

['computer', 'think', 'algorithm', 'office', 'Computer', 'Science']
>>> 
******** ***** ********* ****** ******** *******!
>>> 
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
>>> 
nptDPbHvsxKNWdYyrTqVQRlBCZShzgGjfkJMLmFXwc
>>> 
Group one is "c"
Group two is "omputer"
>>> 
omputercay inkthay algorithmway officeway Omputercay Iencescay!
>>> 
那是冗长的版本。
到拉丁语的定义可以缩短为:

def to_pig_latin(match):
    rest = match.group("rest")
    prefix = match.group("prefix")
    return (str, str.title)[(prefix or rest)[0].isupper()](rest + prefix + "way"[bool(prefix):])

正则表达式起到了拯救作用。在这种情况下,带有单词边界的正则表达式模式将使您的生活更加轻松。单词边界就是它听起来的样子-它表示单词的开始或结束,并在模式中用
\b
表示。在您的情况下,
就是这样一个词的边界。“单词”本身由集合
a-z
a-z
0-9
或下划线中的任何字符组成,在模式中由
\w
表示。
+
表示一个或多个
\w
字符

因此,如果模式是
r“\b\w+\b”
,这将匹配任何单词(由
a-zA-Z0-9
中的任何一个组成),并具有前导或后继单词边界

import re

pattern = r"\b\w+\b"
sentence = "computer think algorithm office Computer Science!"

print(re.findall(pattern, sentence))
输出:

['computer', 'think', 'algorithm', 'office', 'Computer', 'Science']
>>> 
******** ***** ********* ****** ******** *******!
>>> 
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
>>> 
nptDPbHvsxKNWdYyrTqVQRlBCZShzgGjfkJMLmFXwc
>>> 
Group one is "c"
Group two is "omputer"
>>> 
omputercay inkthay algorithmway officeway Omputercay Iencescay!
>>> 
在这里,我们使用
re.findall
来获得匹配该模式的所有子字符串的列表。请注意,不包括空格或标点符号


让我们介绍一下
re.sub
,它需要查找一个模式、一个字符串和另一个字符串来替换它找到的任何匹配项。您可以传递函数,而不是替换字符串。此函数必须将匹配对象作为参数,并且必须返回一个字符串以替换当前匹配

import re

pattern = r"\b\w+\b"
sentence = "computer think algorithm office Computer Science!"

def replace(match):
    return "*" * len(match.group())

print(re.sub(pattern, replace, sentence))
输出:

['computer', 'think', 'algorithm', 'office', 'Computer', 'Science']
>>> 
******** ***** ********* ****** ******** *******!
>>> 
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
>>> 
nptDPbHvsxKNWdYyrTqVQRlBCZShzgGjfkJMLmFXwc
>>> 
Group one is "c"
Group two is "omputer"
>>> 
omputercay inkthay algorithmway officeway Omputercay Iencescay!
>>> 
那只是为了演示的目的


让我们换一下档位:

from string import ascii_letters as alphabet
print(alphabet)
输出:

['computer', 'think', 'algorithm', 'office', 'Computer', 'Science']
>>> 
******** ***** ********* ****** ******** *******!
>>> 
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
>>> 
nptDPbHvsxKNWdYyrTqVQRlBCZShzgGjfkJMLmFXwc
>>> 
Group one is "c"
Group two is "omputer"
>>> 
omputercay inkthay algorithmway officeway Omputercay Iencescay!
>>> 
这对于创建只包含辅音的字符串非常方便:

from string import ascii_letters as alphabet

consonants = "".join(set(alphabet) ^ set("aeiouAEIOU"))
print(consonants)
输出:

['computer', 'think', 'algorithm', 'office', 'Computer', 'Science']
>>> 
******** ***** ********* ****** ******** *******!
>>> 
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
>>> 
nptDPbHvsxKNWdYyrTqVQRlBCZShzgGjfkJMLmFXwc
>>> 
Group one is "c"
Group two is "omputer"
>>> 
omputercay inkthay algorithmway officeway Omputercay Iencescay!
>>> 
我们已经考虑了所有字母字符集和仅元音字符集之间的差异。这只产生一组辅音。请注意,它没有保留在一个集合中的字符顺序,但在我们的例子中这并不重要,因为我们将有效地将这个字符串视为membe的集合测试