当前位置: 动力学知识库 > 问答 > 编程问答 >

python - How to convert lists inside lists to lowercase?

问题描述:

So I have a list

myList1 = [["HELLO HOW ARE YOU", "IM GOOD THANKS"],

["Could you be quiet?","Okay I will be quiet"]]

I would like to know how I can convert every letter to lowercase so that I can run a search against the list to find common words, and then print it back out in original form. For example:

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

Because the code I have tried, only works for lists that don't contain more lists inside of them.!

网友答案:

Use list comprehension to create a separate list with the strings in lower case

>>> [[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']]

This won't modify myList1.

EDIT: According to the OP's question, this is not a feasible solution since OP wishes to compare the lowercase list and then return the result strings in it's original case. Hence, when searching the list, OP should simply convert each string to lowercase using .lower() and then compare it with the search query. No need for a separate list.

网友答案:

store it in another list:

lowerList = [[string.lower() for string in innerList] for innerList in myList1]
网友答案:
myList1 = [["HELLO HOW ARE YOU", "IM GOOD THANKS"],
           ["Could you be quiet?","Okay I will be quiet"]]

eval(str(myList1).lower())
分享给朋友:
您可能感兴趣的文章:
随机阅读: