在python中将字符串转换为小写的简单方法
我有一个文本如下在python中将字符串转换为小写的简单方法,python,Python,我有一个文本如下 mytext = "This is AVGs_ABB and NMN_ABB and most importantly GFD_ABB This is so important that you have to CLEAN the lab everyday" mytext = "this is AVGs_ABB and NMN_ABB and most importantly GFD_ABB this is so important that you have to
mytext = "This is AVGs_ABB and NMN_ABB and most importantly GFD_ABB This is so important that you have to CLEAN the lab everyday"
mytext = "this is AVGs_ABB and NMN_ABB and most importantly GFD_ABB this is so important that you have to clean the lab everyday"
splits = mytext.split()
newtext = []
for item in splits:
if not '_ABB' in item:
item = item.lower()
newtext.append(item)
else:
newtext.append(item)
我想把它转换成小写,除了里面有\u ABB
的单词
因此,我的输出应该如下所示
mytext = "This is AVGs_ABB and NMN_ABB and most importantly GFD_ABB This is so important that you have to CLEAN the lab everyday"
mytext = "this is AVGs_ABB and NMN_ABB and most importantly GFD_ABB this is so important that you have to clean the lab everyday"
splits = mytext.split()
newtext = []
for item in splits:
if not '_ABB' in item:
item = item.lower()
newtext.append(item)
else:
newtext.append(item)
我目前的代码如下
mytext = "This is AVGs_ABB and NMN_ABB and most importantly GFD_ABB This is so important that you have to CLEAN the lab everyday"
mytext = "this is AVGs_ABB and NMN_ABB and most importantly GFD_ABB this is so important that you have to clean the lab everyday"
splits = mytext.split()
newtext = []
for item in splits:
if not '_ABB' in item:
item = item.lower()
newtext.append(item)
else:
newtext.append(item)
但是,我想知道是否有简单的方法可以做到这一点,可能是一行?您可以使用一行代码将字符串拆分为单词,使用
str.endswith()
检查单词,然后将单词重新连接在一起:
' '.join(w if w.endswith('_ABB') else w.lower() for w in mytext.split())
# 'this is AVGs_ABB and NMN_ABB and most importantly GFD_ABB this is so important that you have to clean the lab everyday'
当然,如果
“u ABB”
实际上可以出现在单词中的任何位置,而不仅仅是在末尾,则使用in
运算符而不是str.endswith()
。您可以使用一行程序将字符串拆分为单词,用str.endswith()
检查单词,然后将单词重新连接在一起:
' '.join(w if w.endswith('_ABB') else w.lower() for w in mytext.split())
# 'this is AVGs_ABB and NMN_ABB and most importantly GFD_ABB this is so important that you have to clean the lab everyday'
当然,如果“ABB”
实际上可以出现在单词中的任何位置,而不仅仅是在末尾,则使用in
运算符而不是str.endswith()
。扩展正则表达式方法:
import re
mytext = "This is AVGs_ABB and NMN_ABB and most importantly GFD_ABB This is so important that you have to CLEAN the lab everyday"
result = re.sub(r'\b((?!_ABB)\S)+\b', lambda m: m.group().lower(), mytext)
print(result)
输出:
this is AVGs_ABB and NMN_ABB and most importantly GFD_ABB this is so important that you have to clean the lab everyday
详情:
-单词边界\b
-先行否定断言,确保给定模式不匹配(?!\u ABB)
-非空白字符\S
-整个模式与不包含子字符串的单词匹配\b((?!\u ABB)\S)+\b
\u ABB
import re
mytext = "This is AVGs_ABB and NMN_ABB and most importantly GFD_ABB This is so important that you have to CLEAN the lab everyday"
result = re.sub(r'\b((?!_ABB)\S)+\b', lambda m: m.group().lower(), mytext)
print(result)
输出:
this is AVGs_ABB and NMN_ABB and most importantly GFD_ABB this is so important that you have to clean the lab everyday
详情:
-单词边界\b
-先行否定断言,确保给定模式不匹配(?!\u ABB)
-非空白字符\S
-整个模式与不包含子字符串的单词匹配\b((?!\u ABB)\S)+\b
\u ABB
mytext = "This is AVGs_ABB and NMN_ABB and most importantly GFD_ABB This is so important that you have to CLEAN the lab everyday"
print(' '.join(map(lambda x : x if '_ABB' in x else x.lower(), mytext.split())))
哪些产出:
this is AVGs_ABB and NMN_ABB and most importantly GFD_ABB this is so important that you have to clean the lab everyday
注意:这假设您的文本将只按空格分隔单词,因此split()
在这里就足够了。如果文本包含标点符号,如,!。“
,则需要使用正则表达式来拆分单词 这里是另一个可能的(不优雅的)一行:
mytext = "This is AVGs_ABB and NMN_ABB and most importantly GFD_ABB This is so important that you have to CLEAN the lab everyday"
print(' '.join(map(lambda x : x if '_ABB' in x else x.lower(), mytext.split())))
哪些产出:
this is AVGs_ABB and NMN_ABB and most importantly GFD_ABB this is so important that you have to clean the lab everyday
注意:这假设您的文本将只按空格分隔单词,因此
split()
在这里就足够了。如果文本包含标点符号,如,!。“
,则需要使用正则表达式来拆分单词 当然,我们可以为此发明一行程序,但您的版本到底出了什么问题?“它更具可读性。”肯达斯,我不会这么说;没有人会期望看到那么多用于该任务的代码。创建太多的变量mytext.replace(“\u ABB”,“\u ABB”)
当然,可以为此发明一个行程序,但您的版本到底有什么问题?“它更具可读性。”肯达斯,我不会这么说;没有人会期望看到那么多用于该任务的代码。创建的变量太多。替换(“\u ABB”,“\u ABB”)可能会使用。。。如果w
中有“_ABB”,因为问题要求“单词中有_ABB”。它不必在末尾,如果w中的''u ABB',我会把它写成,这样它就适用于OP所要求的单词,这些单词中有\u ABB
可能会使用。。。如果w
中有“_ABB”,因为问题要求“单词中有_ABB”。它不必在末尾,我会把它写成如果w
中的''u ABB',这样它就适用于OP所要求的单词中有\u ABB