扫码关注官方订阅号
比如将list:[3, 8, 9, 4, 1, 10, 6, 7, 2, 5] 按照下标顺序分成3组:[3, 8, 9] [4, 1, 10] [6, 7, 2, 5] 或分成4组:[3, 8] [9, 4] [1, 10, 6] [7, 2, 5]
认证0级讲师
pythona=[1,2,3,4,5,6,7,8,9,10] for i in range(0,len(a),3): b=a[i:i+3] print b
python
a=[1,2,3,4,5,6,7,8,9,10] for i in range(0,len(a),3): b=a[i:i+3] print b
python[a[i:i+3] for i in xrange(0,len(a),3)]
[a[i:i+3] for i in xrange(0,len(a),3)]
不知道算不算优雅?
我也不知道有啥技巧,等待其他人来回答。 第一题我的做法
#!/usr/bin/python L = [3,8,9,4,1,10,6,7,2,5] result = [[],[],[]] #for i, item in enumerate(L): for item in L: if len(result[0]) < 3: result[0].append(item) elif len(result[1]) < 3: result[1].append(item) else: result[2].append(item) print result
自己写个函数吧
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
直接上代码:
如果喜欢简洁可以这样:
不知道算不算优雅?
我也不知道有啥技巧,等待其他人来回答。
第一题我的做法
自己写个函数吧