python removing whitespace from string in a list

前端 未结 6 997
名媛妹妹
名媛妹妹 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:15

    You don't need to count i, j yourself, just enumerate, also looks like you do not increment i, as it is out of loop and j is not in inner most loop, that is why you have an error

    for x in networks:
        for i, y in enumerate(x):
            x[i] = y.strip()
    

    Also note you don't need to access networks but accessing 'x' and replacing value would work, as x already points to networks[index]

提交回复
热议问题