Python-基础算法之二维数组90度旋转
Python-基础算法之二维数组90度旋转
http://www.cnblogs.com/alex3714/articles/5143440.html
要求:生成一个4*4的2维数组并将其顺时针旋转90度
>>> a = [] >>> for i in range(10): ... a.append(i) ... >>> print(a) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> b = [ i for i in range(10)] >>> b [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> >>> a = [[col for col in range(4)] for raw in range(4)] >>> a [[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]] >>> for i in a: ... print(i) ... [0, 1, 2, 3] [0, 1, 2, 3] [0, 1, 2, 3] [0, 1, 2, 3] >>> data = [[col for col in range(4)] for row in range(4)] for row in data: print(row) print('--------------') for i in range(len(data)): #print(i) a = [data[i][i] for row in range(4)] print(a) /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/admin/PycharmProjects/s18/day3/二分法.py [0, 1, 2, 3] [0, 1, 2, 3] [0, 1, 2, 3] [0, 1, 2, 3] -------------- [0, 0, 0, 0] [1, 1, 1, 1] [2, 2, 2, 2] [3, 3, 3, 3] http://www.jb51.net/article/53051.htm a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # c = [ [row[col] for row in a] for col in range(len(a[0]))] # print(c) for col in range(len(a)): b = [] c = [] for row in range(len(a)): c.append(a[row][col]) b.append(c) print(b) /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/admin/PycharmProjects/s18/day3/二分法.py [[1, 4, 7]] [[2, 5, 8]] [[3, 6, 9]]