用列表推导式如何生成如下列表:[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
inner_list = []outer_list = []for i in range(1,10): inner_list.append(i) if i % 3 == 0: outer_list.append(inner_list) inner_list = []print(outer_list)
运行
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
本文共 256 字,大约阅读时间需要 1 分钟。
用列表推导式如何生成如下列表:[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
inner_list = []outer_list = []for i in range(1,10): inner_list.append(i) if i % 3 == 0: outer_list.append(inner_list) inner_list = []print(outer_list)
运行
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
转载地址:http://jxsox.baihongyu.com/