尝试解决以下问题:
我有一个大型集合,我将通过以下示例列表表示:
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
如何拼接此列表并为列表中的每四个元素返回一系列分隔字符串?
期望的输出:
1, 2, 3, 4
5, 6, 7, 8
9, 10, 11, 12
13, 14, 15, 16
答案 0 :(得分:0)
你可以试试这个:
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
new_list = [l[i:i+4] for i in range(0, len(l), 4)]
输出:
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]