How to modify list entries during for loop?

前端 未结 9 886
旧巷少年郎
旧巷少年郎 2020-11-22 01:56

Now I know that it is not safe to modify the list during an iterative looping. However, suppose I have a list of strings, and I want to strip the strings themselves. Does re

9条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 02:18

    You can do something like this:

    a = [1,2,3,4,5]
    b = [i**2 for i in a]
    

    It's called a list comprehension, to make it easier for you to loop inside a list.

提交回复
热议问题