简体   繁体   English

Python暂时将字母改为小写

[英]Python temporarily change letter to lower case

I have a list that looks like this: 我有一个如下所示的列表:

separate=[['I','wnet','to','the','mal,'],['and','bouht','a','new','shirt.'],['What','did','you','do','todya?']]

I'm going to run this through a program which will identify the misspelled words and replace them with the correct word but the list of words in the Webster Dictionary that I am using only uses lowercase letters. 我将通过一个程序来运行它,该程序将识别拼写错误的单词并用正确的单词替换它们,但我使用的Webster Dictionary中的单词列表仅使用小写字母。 Can I temporarily change all of the letters to lowercase but then in the end return the original upper and lower case words? 我可以暂时将所有字母更改为小写但最后返回原始的大写和小写字母吗?

I know about str.upper() and str.lower() and set.capitalize . 我知道str.upper()str.lower()以及set.capitalize

It seems like I want to use something like str.capwords() but inversely... I want to split the list into words(already done) and then make capital letters lower case. 似乎我想使用像str.capwords()这样的东西,但str.capwords() ...我想将列表拆分成单词(已经完成),然后将大写字母str.capwords()

Just use str.lower . 只需使用str.lower It will return a new string with lower letter and keep the original string unchanged. 它将返回一个字母较小的新字符串,并保持原始字符串不变。 So when you loop through the list such as if string.lower() in adict: , you do use the lower case to compare and the original string unchanged. 所以当你循环遍历列表时,比如if string.lower() in adict: ,你会使用小写进行比较,原始字符串不变。

>>> a = 'ABC'
>>> a.lower()
'abc'
>>> a
'ABC'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM