python removing whitespace from string in a list

前端 未结 6 967
名媛妹妹
名媛妹妹 2021-01-18 10:24

I have a list of lists. I want to remove the leading and trailing spaces from them. The strip() method returns a copy of the string without leading and trailing

6条回答
  •  独厮守ぢ
    2021-01-18 11:21

    You're forgetting to reset j to zero after iterating through the first list.

    Which is one reason why you usually don't use explicit iteration in Python - let Python handle the iterating for you:

    >>> networks = [["  kjhk  ", "kjhk  "], ["kjhkj   ", "   jkh"]]
    >>> result = [[s.strip() for s in inner] for inner in networks]
    >>> result
    [['kjhk', 'kjhk'], ['kjhkj', 'jkh']]
    

提交回复
热议问题