怎么把二维数组转换成一维:
比如把 [[1,2], [3,4], [5,6]] 转换为 [1, 2, 3, 4, 5, 6]
代码如下:
list1 = [j for i in [[1,2], [3,4], [5,6]] for j in i]
print("list1 = ", list1) # list1 = [1, 2, 3, 4, 5, 6]
#思路如下:
for i in [[1,2], [3,4], [5,6]]: # 先降维得到[1,2], [3,4], [5,6]
#for j in i 共享3次迭代的步骤, 所以就可以把从[1,2], [3,4], [5,6]中的抽离出来
#print(i)
#print(type(i))
for j in i:
print("j = ", j)
'''
j = 1
j = 2
j = 3
j = 4
j = 5
j = 6
'''
#list2 = [j for i in [[1,2], [3,4], [5,6]]] # error j 没有定义 is not defined