Skip to main content
 首页 » 编程设计

python之如何将列表中的列表转换为小写

2025年05月04日2498°冷暖

所以我有一个列表

myList1 = [["HELLO HOW ARE YOU", "IM GOOD THANKS"], 
           ["Could you be quiet?","Okay I will be quiet"]] 

我想知道如何将每个字母都转换为小写,以便我可以对列表运行搜索以查找常用词,然后以原始形式将其打印出来。例如:

some code to convert list to lowercase 
some code to search for a term 
some code to convert list to original form 
some code to print all matches 

因为我试过的代码,只适用于其中不包含更多列表的列表。!

请您参考如下方法:

使用列表推导式创建一个单独的列表,其中的字符串为小写

>>> [[j.lower() for j in i] for i in myList1] 
[['hello how are you', 'im good thanks'], ['could you be quiet?', 'okay i will be quiet']] 

这不会修改 myList1。

编辑:根据 OP 的问题,这不是可行的解决方案,因为 OP 希望比较小写列表,然后返回原始大小写的结果字符串。因此,在搜索列表时,OP 应该使用 .lower() 简单地将每个字符串转换为小写,然后将其与搜索查询进行比较。无需单独的列表。