Python: list items are not changed in for loop

前端 未结 3 2028
無奈伤痛
無奈伤痛 2020-12-12 07:33

I was shocked when the following code did not do what I expected it to do:

lines_list = [\'this is line 1\\n\', \'this is line 2\\n\', \'this is line 3\\n\']         


        
3条回答
  •  长情又很酷
    2020-12-12 08:15

    You did forget to assigned new value (stripped string) to something.

    Another way, how to assign it to a list in shorter way is:

    >>> lst = ['this is line 1\n', 'this is line 2\n', 'this is line 3\n']
    >>> import string
    >>> lst = map(string.strip, lst)
    >>> lst
    ['this is line 1', 'this is line 2', 'this is line 3]
    

提交回复
热议问题