Python 一维列表转换成二维
# 将一维列表转为二维列表,每行元素个数为n n = 3 one_dim_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] two_dim_list = [one_dim_list[i:i+n] for i in range(0, len(one_dim_list), n)] print(two_dim_list) # 输出:[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# 将一维列表转为二维列表,每行元素个数为n n = 3 one_dim_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] two_dim_list = [one_dim_list[i:i+n] for i in range(0, len(one_dim_list), n)] print(two_dim_list) # 输出:[[1, 2, 3], [4, 5, 6], [7, 8, 9]]