Python3 小技巧三 每十个数取一次值

题:元组有99个元素,我要每10个取一次 获得[xx,xx,xx,xx,...]

答案形式:[1-10]  [11-20]  [21-30] ....

a = [1,2,3,4,5,6,7,8,9,10,11]
step = 3
b = [a[i:i+step] for i in range(0,len(a),step)]
print(b)


>>> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11]]
a = [1,2,3,4,5,6,7,8,9,10,11]
step = 3
for i in range(0,len(a),step):
   print(a[i:i+step])




>>>  [1, 2, 3]
     [4, 5, 6]
     [7, 8, 9]
     [10, 11]

注解:a[i:i+setp] 是对a列表进行切片,i是for语句取出的间隔值,i用于a切片的起点,i+setp是终点

感谢  Mr-Cat踏雪三郎给予提示,原网址:https://blog.csdn.net/mr_cat123/article/details/80584988

你可能感兴趣的:(python,小技巧)