1

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.!

oneman
  • 811
  • 1
  • 13
  • 31
  • You will have to use *two* levels: the first to get each sublist in `myList1`, the second to get each string in the sublist. Then you can lowercase the string. –  May 02 '16 at 07:03

4 Answers4

5

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.

JRodDynamite
  • 12,325
  • 5
  • 43
  • 63
  • If I store it in another list, how will the search work? Because I will be searching for matches in myList2, but I want to print the matches from myList1 – oneman May 02 '16 at 07:11
  • @LiamEmery - Store the index value of the matches and then at the end fetch the result from `myList1`. – JRodDynamite May 02 '16 at 07:12
  • You will have to elaborate sorry, as you can probably tell, I'm very new to python – oneman May 02 '16 at 07:16
  • @LiamEmery - Discard what I said previously, whenever you fetch a string from the list and match it, before matching convert the string to lowercase using `.lower()` and then match. – JRodDynamite May 02 '16 at 07:19
  • @LiamEmery - You don't exactly need to create a separate list for this purpose. – JRodDynamite May 02 '16 at 07:19
  • There is a .lower() function and a .upper() function. Is there something similar to make the first letter in a sentence uppercase, the first letter after a full stop uppercase, and so on? – oneman May 02 '16 at 07:22
  • @LiamEmery - That requires a little bit of NLP. Have a look at this [answer](http://stackoverflow.com/a/22800751/2932244). I think this should help. – JRodDynamite May 02 '16 at 07:33
0

store it in another list:

lowerList = [[string.lower() for string in innerList] for innerList in myList1]
jtitusj
  • 3,046
  • 3
  • 24
  • 40
0
myList1 = [["HELLO HOW ARE YOU", "IM GOOD THANKS"],
           ["Could you be quiet?","Okay I will be quiet"]]

eval(str(myList1).lower())
SuperNova
  • 25,512
  • 7
  • 93
  • 64
0

I struggled with the same problem , but was finally able to find a solution.And I am happy that i could contribute .Now back to question , what you are looking for is new_list = np.char.lower(name of your list) i.e new_list = np.char.lower(my_List1).

This is the documentation link https://numpy.org/doc/stable/reference/generated/numpy.char.lower.html

I hope this answer helps you.This was my first contribution .Will keep Continuing.

devansh
  • 89
  • 2
  • 8