所以我有一个清单
myList1 = [["HELLO HOW ARE YOU", "IM GOOD THANKS"],
["Could you be quiet?","Okay I will be quiet"]]
Run Code Online (Sandbox Code Playgroud)
我想知道如何将每个字母转换为小写,以便我可以对列表进行搜索以查找常用字词,然后以原始格式将其打印出来.例如:
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
Run Code Online (Sandbox Code Playgroud)
因为我尝试过的代码只适用于其中不包含更多列表的列表.
使用list comprehension创建一个单独的列表,其中包含小写字符串
>>> [[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']]
Run Code Online (Sandbox Code Playgroud)
这不会修改myList1.
编辑:根据OP的问题,这不是一个可行的解决方案,因为OP希望比较小写列表,然后返回结果字符串的原始情况.因此,在搜索列表时,OP应该简单地将每个字符串转换为小写.lower(),然后将其与搜索查询进行比较.无需单独列表.
| 归档时间: |
|
| 查看次数: |
1283 次 |
| 最近记录: |