需求:将列表[0,1,2,3,4,5,6,7,8,9]的每一个元素加1
方法一:
#Author:Anliu
a = [0,2,3,4,5,6,7,8,9]
b = []
for i in a:b.append(i+1)
a=b
print(a)
方法二:
#Author:Anliu a = [0,2,3,4,5,6,7,8,9] for index,i in enumerate(a): a[index] +=1 print(a)
方法三:
a = [i+1 for i in range(10)]
print(a)
这就叫做列表生成器
原文:https://www.cnblogs.com/anttech/p/12656590.html