• python学习_1


    numpy.array[  ]

    #!/usr/bin/python3
    # -*- coding: utf-8 -*-
    # @Time    : 18-10-17 上午10:51
    # @Author  : Guo Zhengbing
    # @Email   : cn_gzb@126.com
    
    import numpy
    
    # print(help(numpy.genfromtxt))
    a = numpy.array([5, 10, 25, 30])
    b = numpy.array([[5, 1, 2], [23, 4, 9]])
    # print(a)
    # print(b) # 输出矩阵
    
    # print(a.shape) # 输出这个矩阵的类型 N行M列
    # print(b.shape)
    
    # numbers = numpy.array([1, 2, 3, 4, '5'])
    # print(numbers)
    # print(numbers.dtype) # 输出类型
    # 保证同一类型
    
    # print(a[0:3]) # 矩阵切片
    
    # print(b[:, 0]) # 取一个矩阵第1列
    # print(b[:, 0: 2]) # 取一个矩阵前两列
    
    matrix = numpy.array([
                   [5, 10, 15],
                   [3, 6, 9],
                   [8, 7, 13]
                ])
    # print(matrix[1:3, 0:2]) # 取几行几列
    
    a = numpy.array([5, 10, 25, 30])
    # a == 10
    # print(a == 10) # 判断数组中各个元素是否是10
    # print(matrix == 10) # 判断数组中各个元素是否是10
    
    # equal = (a == 10)
    # print(equal)
    # print(a[equal]) #返回真实值
    import numpy
    # numpy中&(与) | 的用法
    a = numpy.array([5, 10, 10, 20])
    # equal = (a == 5) & (a == 10)
    # print(equal)
    # equal = (a == 5) | (a == 10)
    # print(equal)
    
    b = numpy.array(["1", "101", "20"])
    c = numpy.array([2, 8, 10])
    # print(b.dtype)
    # print(b)
    # b = b.astype(float)  # 将b中元素变成float型
    # print(b.dtype)
    # print(b)
    # print(c.dtype)
    print(c.min())
    d = numpy.array([[58, 11, 2],
                     [23, 43, 9],
                     [20, 25, 3]
                     ])
    print(d.sum(axis=1))  # 对每个行求和
    print(d.sum(axis=0))   # 对每一个列求和
    import numpy as np
    print(np.arange(15))
    a = np.arange(15).reshape(3, 5)   # 把行向量变成3行5列的举证
    print(a)
    print(a.shape)   # 打印行和列分别为多少
    print(a.ndim)     # 打印维度
    print(a.dtype.name)   # 打印类型
    print(a.size)   # 打印数组中元素个数

    output:
    [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
    [[ 0  1  2  3  4]
     [ 5  6  7  8  9]
     [10 11 12 13 14]]
    (3, 5)
    2
    int64
    15

    import numpy
    array = numpy.zeros((3, 4))  # 构造一个m*n维的0矩阵
    print(array)
    
    [[0. 0. 0. 0.]
     [0. 0. 0. 0.]
     [0. 0. 0. 0.]]
    import numpy as np
    print(np.ones((2, 3, 4), dtype=np.int32))
    
    
    [[1 1 1 1]
      [1 1 1 1]
      [1 1 1 1]]]
    import numpy as np
    # print(np.ones((2, 3, 4), dtype=np.int32))
    print(np.arange(10, 32, 5))  # 输出一个这样的数组:从第一个数开始累加5,但是结果小于32
    
    
    [10 15 20 25 30]
    import numpy as np
    print(np.arange(12).reshape(4, 3))   # 构造一个矩阵:0-11,四行三列
    
    
    [[ 0  1  2]
     [ 3  4  5]
     [ 6  7  8]
     [ 9 10 11]]
    生成随机种子:
    import numpy as np
    print(np.random.rand(2, 3))  # 两行三列的随机种子  从 0 -- +1 上的值(在python3.6中)
    
    
    [[0.36246585 0.01616546 0.06700026]
     [0.14067238 0.24054495 0.60194616]]
    import numpy as np
    from numpy import pi
    a = np.linspace(0, 2*pi, 100)  # 起始值为0,终点为2*pi,平均取100个值
    print(a)
    
    
    
    [0.         0.06346652 0.12693304 0.19039955 0.25386607 0.31733259
     0.38079911 0.44426563 0.50773215 0.57119866 0.63466518 0.6981317
     0.76159822 0.82506474 0.88853126 0.95199777 1.01546429 1.07893081
     1.14239733 1.20586385 1.26933037 1.33279688 1.3962634  1.45972992
     1.52319644 1.58666296 1.65012947 1.71359599 1.77706251 1.84052903
     1.90399555 1.96746207 2.03092858 2.0943951  2.15786162 2.22132814
     2.28479466 2.34826118 2.41172769 2.47519421 2.53866073 2.60212725
     2.66559377 2.72906028 2.7925268  2.85599332 2.91945984 2.98292636
     3.04639288 3.10985939 3.17332591 3.23679243 3.30025895 3.36372547
     3.42719199 3.4906585  3.55412502 3.61759154 3.68105806 3.74452458
     3.8079911  3.87145761 3.93492413 3.99839065 4.06185717 4.12532369
     4.1887902  4.25225672 4.31572324 4.37918976 4.44265628 4.5061228
     4.56958931 4.63305583 4.69652235 4.75998887 4.82345539 4.88692191
     4.95038842 5.01385494 5.07732146 5.14078798 5.2042545  5.26772102
     5.33118753 5.39465405 5.45812057 5.52158709 5.58505361 5.64852012
     5.71198664 5.77545316 5.83891968 5.9023862  5.96585272 6.02931923
     6.09278575 6.15625227 6.21971879 6.28318531]
    import numpy as np
    a = np.array([20, 30, 40, 50])
    print(a<35)   # 判断大小
    
    
    [ True  True False False]
    import numpy as np
    A = np.array([[1, 2],
                  [0, 3]
                  ])
    B = np.array([[2, 0],
                  [1, 3]
                  ])
    print(A*B)   # 相对位置相乘
    print(A.dot(B))  # 矩阵乘法
    print(np.dot(A, B))
    
    
    [[2 0]
     [0 9]]
    [[4 6]
     [3 9]]
    [[4 6]
     [3 9]]
    import numpy as np
    a = np.transpose([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
    print(a)
    b = np.transpose(a).tolist()
    c = a.T
    print(b)
    print(c)
    
    
    
    [[1 1 1]
     [2 2 2]
     [3 3 3]]
    [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
    [[1 2 3]
     [1 2 3]
     [1 2 3]]
  • 相关阅读:
    Codeforces Round #665 (Div. 2) C. Mere Array 数论,思维
    Codeforces Round #665 (Div. 2) B
    ZOJ
    HDU-2158 最短区间版大家来找茬 模拟 尺取
    HDU-1082 排列组合 普通生成函数 细节
    HDU
    poj-1651 multiplication puzzle(区间dp)
    hdu-1231 连续最大子序列(动态规划)
    poj-2488 a knight's journey(搜索题)
    hdu-2063 过山车(二分图)
  • 原文地址:https://www.cnblogs.com/cn-gzb/p/9803193.html
Copyright © 2020-2023  润新知