现在有个需求,看列表【0,1,2,3,4,5,6,7,8,9】,要求你把列表里的每件值 加1,你怎么实现?你可能会想到2种方式
二逼表年版
a = [0,1,2,3,4,5,6,7,8,9]
b = []
for i in a:b.append(i+1)
a = b
a
[1,2,3,4,5,6,7,8,9,10]
普通青年版
a = [1,3,4,6,7,7,8,9,11]
fro index,i in enumerate(a):
a[index] +=1
print(a)
文艺青年版
a = [1,2,3,4,5,6,7,8,9,10]
a =map(lambda x:x+1,a)
for i in a:print(i)
装逼青年版
a =[i+1 for i in range(10)]
列表生成式
>>> a
[0, 1, 4, 9, 16, 25, 6, 7, 8, 9]
>>> a = [i if i >5 else i*i for i in a] #列表生成式
>>> a
[0, 1, 4, 9, 16, 25, 6, 7, 8, 9]