字符与python之间的大写字母
我需要帮助我如何得到两颗星之间的上限 输入:字符与python之间的大写字母,python,python-3.x,uppercase,Python,Python 3.x,Uppercase,我需要帮助我如何得到两颗星之间的上限 输入:“S*t*a*r*S为每个*位置*” 输出:“星星无处不在” 我的代码在这里: def trans(s): x = "" a = False for j in range(len(s)): if s[j] == "*" or a: a = True if a: x += s[j].upper() else:
“S*t*a*r*S为每个*位置*”
输出:“星星无处不在”
我的代码在这里:
def trans(s):
x = ""
a = False
for j in range(len(s)):
if s[j] == "*" or a:
a = True
if a:
x += s[j].upper()
else:
x += s[j]
return "".join(x.replace("*",""))
问题是我不知道循环中的哪个位置设置回False
。现在它只看到*
,并将所有内容都大写
注意:其他答案很好地向您展示了如何修复代码。这里有另一种方法,学习正则表达式后,您可能会发现这一方法更简单 您可以使用
re.sub
功能
>>> s = "S*t*a*r*s are every*where*"
>>> re.sub(r'\*([^*]*)\*', lambda m: m.group(1).upper(), s)
'STaRs are everyWHERE'
在regex*
中,是一个特殊的元字符,它将前一个标记重复零次或多次。为了匹配文本*
,您需要在正则表达式中使用\*
因此,\*([^*]*)\*
regex匹配每对*
块,即(*t*
,*r*
,*其中*
)和中间字符(存在于*
块中的字符)由组1捕获
对于每个匹配,re.sub
函数将用-*.upper()内的字符串替换匹配的*..*
块。也就是说,它将对*
中的字符串应用upper()
函数,并将结果作为替换字符串返回。您需要切换状态;每次找到*
时,都会反转函数的状态,以便在遍历文本时可以在大写和小写之间切换
使用而不是最容易做到这一点<如果代码>非
为
假
则返回真,反之亦然:
def trans(s):
x = ""
a = False
for j in range(len(s)):
if s[j] == "*":
a = not a # change state; false to true and true to false
continue # no need to add the star to the output
if a:
x += s[j].upper()
else:
x += s[j]
return x
每次您找到一个*
字符时,都会切换a
;通过在此时使用continue
,还可以防止将*
字符添加到输出中,因此可以完全避免使用replace()
。对字符串的'.join()
调用再次生成相同的字符串,在这种情况下不需要它
这里不需要range()
,您可以直接循环s
。您也可以使用更好的名称:
def trans(string):
result = ""
do_upper = False
for character in string:
if character == "*":
do_upper = not do_upper # change state; false to true and true to false
continue # no need to add the star to the output
result += character.upper() if do_upper else character
return result
演示:
这样想吧。每当你看到一个
*
,你需要在大写和原大写之间交替。所以,在代码中实现相同的功能,如下所示
def trans(s):
x, flag = "", False
# You can iterate the string object with `for`
for char in s:
# The current character is a `*`
if char == "*":
# flip the flag everytime you see a `*`.
flag = not flag
# Skip further processing, as the current character is `*`
continue
if flag:
# If the flag is Truthy, we need to uppercase the string
x += char.upper()
else:
# Otherwise add the character as it is to the result.
x += char
# no need to `join` and `replace`, as we already skipped `*`. So just return.
return x
a
应在True
和False
之间切换。您仅将其设置为True
。也可以直接在字符串的字符上迭代,而不是在索引上迭代。并使用更全面的变量名。如果立即跳过“*”,则不需要联接,也不需要替换:
def trans(text):
result = ""
upper = False
for char in text:
if char == "*":
upper = not upper
elif upper:
result += char.upper()
else:
result += char
return result
在星体中,“t”和“r”也被改为大写字母???@upaangsaxena:是的,因为它们直接跟在星体后面。这是一个非常著名的自动机。若要理解答案,请检查此图像@BhargavRao:我已在线添加了您的图片。@J.F.Sebastian Tnx you,现在我明白了:)并非所有内容都需要正则表达式。这也不能解释OP哪里出了问题。Tnx,但我只是从常规的体验开始,所以我不太擅长them@AvinashRaj:我相信你知道,懒惰是一种美德我没有否决投票,但我同意,如果能找到简单的非正则表达式解决方案,就应该避免使用正则表达式,部分原因是正则表达式倾向于阻碍可读性,部分原因是正则表达式解决方案不一定有效。我想这些评论不是讨论这个问题的地方,但请随意提出来。@PM2Ring:为什么您认为手工实现FSM比指定简单的正则表达式更好?@PM2Ring:据我所知,您的理由是:(1)与手工编写的FSM相比,正则表达式阻碍了可读性(2)regex比手工编写的FSM(3)学习循环、条件、一个简单的FSM而不是regex慢。我同意(3)(这可以被认为是掌握正则表达式的先决条件)。我非常怀疑(2)除非剖析器证明不是这样的,(1):一旦你学会了正则表达式,
\*([^*]*)\*
是一种更简短、更清晰的表达问题的方式。每个程序员都应该学习正则表达式(这并不意味着它们应该在任何地方使用:正则表达式经常(ab)超出其使用范围)为了避免依赖CPython优化来获得线性行为,您可以使用result=[]
和result.append(…)
而不是result=“
和result+=
。并返回“”。在末尾加入(结果)
。
def trans(text):
result = ""
upper = False
for char in text:
if char == "*":
upper = not upper
elif upper:
result += char.upper()
else:
result += char
return result