Editing strings in a list of lists

后端 未结 5 707
一向
一向 2021-01-23 08:50

I\'ve made a list of lists from a text block where each list contains all the words in a line as a separate element, like this:

listoflists = [[\'Lorem\', \'ipsu         


        
5条回答
  •  星月不相逢
    2021-01-23 09:01

    listoflists = [['Lorem', 'ipsum', 'dolor', 'sit', 'amet\n'],['consectetur', 'adipiscing', 'elit', 'donec', 'iaculis\n']]
    for l in listoflists:
        l[-1] = l[-1].replace('\n','')
    print listoflists
    

    """ Output:

    [['Lorem', 'ipsum', 'dolor', 'sit', 'amet'], ['consectetur', 'adipiscing', 'elit', 'donec', 'iaculis']]

    """

提交回复
热议问题