• API
• FAQ
• Tools
• Trends
• Archive
daily pastebin goal
32%
SHARE
TWEET

# Untitled

a guest Feb 17th, 2017 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. ls = [1, 2, 3, 4, 5, 6, 7, 8, 9]
2.
3. ls = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
4.
5. >>> nls=[]
6. >>> for i in range(0,3):nls.append(ls[3*i:3*i+3])
7. >>> nls
8. [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
9.
10. In [132]: ls = [1, 2, 3, 4, 5, 6, 7, 8, 9]
11.
12. In [133]: [ls[i*3:i*3+3] for i in range((len(ls)//3 + (len(ls)%3>0)*1))]
13. Out[133]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
14.
15. In [134]: ls = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
16.
17. In [135]: [ls[i*3:i*3+3] for i in range((len(ls)//3 + (len(ls)%3>0)*1))]
18. Out[135]: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
19.
20. In [136]: ls = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
21.
22. In [137]: [ls[i*3:i*3+3] for i in range((len(ls)//3 + (len(ls)%3>0)*1))]
23. Out[137]: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11]]
24.
25. In [138]: ls = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
26.
27. In [139]: [ls[i*3:i*3+3] for i in range((len(ls)//3 + (len(ls)%3>0)*1))]
28. Out[139]: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
29.
30. result = [ls[i:i+3] for i in range(0, len(ls), 3)]
RAW Paste Data
Top