代码: #!/usr/bin/env python # encoding: utf-8 """ @author: 侠之大者kamil @file: 2.回形矩阵.py @time: 2016/3/21 10:51 """ def sankeMatric(n): m = [[0] * n for x in range(n)] p = 0 q = n-1 t = 1 while p < q: for i in range(p,q): m[p][
def bsm(n): a = [[0]*n for x in range(n)] p = 0 q = n-1 t = 1 while p < q: for i in range(p,q): a[p][i] = t t += 1 for i in range(p,q): a[i][q] = t t += 1 for i in range(q,p,-1): a[q][i] = t t += 1 for i in range(q,p,-1): a[i][p] = t t += 1 p += 1 q
python实现矩阵乘法的方法 本文实例讲述了python实现矩阵乘法的方法.分享给大家供大家参考. 具体实现方法如下: def matrixMul(A, B): res = [[0] * len(B[0]) for i in range(len(A))] for i in range(len(A)): for j in range(len(B[0])): for k in range(len(B)): res[i][j] = A[i][k] *
前几天群里有同学提出了一个问题:手头现在有个列表,列表里面两个元素,比如[1, 2],之后不断的添加新的列表,往原来相应位置添加.例如添加[3, 4]使原列表扩充为[[1, 3], [2, 4]],再添加[5, 6]扩充为[[1, 3, 5], [2, 4, 6]]等等. 其实不动脑筋的话,用个二重循环很容易写出来: def trans(m): a = [[] for i in m[0]] for i in m: for j in range(len(i)): a[j].append(i[j])
前几天群里有同学提出了一个问题:手头现在有个列表,列表里面两个元素,比如[1, 2],之后不断的添加新的列表,往原来相应位置添加.例如添加[3, 4]使原列表扩充为[[1, 3], [2, 4]],再添加[5, 6]扩充为[[1, 3, 5], [2, 4, 6]]等等. 其实不动脑筋的话,用个二重循环很容易写出来: def trans(m): a = [[] for i in m[0]] for i in m: for j in range(len(i)): a[j].append(i[j])
计算N×M(建议维度大于100*100)的0,1矩阵均匀分布程度,值由0到1表示不均匀到均匀 import numpy as np def make_rand_matrix(side=20): # 制作随机矩阵,用于测试 a = np.random.random((side,side)) for i in range(0,side): for j in range(0,side): if a[i,j]>0.3: a[i,j] = 1 else: a[i,j] = 0 return a def g