How to convert elements in a list of list to lowercase?

后端 未结 3 1470
既然无缘
既然无缘 2021-01-16 09:04

I am trying to convert the elements of a list of list of lowercase. This is what is looks like.

print(dataset)
[[\'It\', \'went\', \'Through\', \'my\', \'shi         


        
3条回答
  •  庸人自扰
    2021-01-16 09:32

    Either use map

    map(str.lower,line)
    

    Or list comprehension (which is basically syntactic sugar)

    [x.lower() for x in line]
    

    And this process can be nested for the entire dataset

    [[x.lower() for x in line] for line in dataset]
    

    And if you want to join all lines into one, use reduce:

    reduce(list.__add__,[[x.lower() for x in line] for line in dataset])
    

提交回复
热议问题