1. 逻辑运算
知识点
程序如下:
# coding=utf-8
from numpy import array,random
#--------------逻辑-------------#
print(1.0==1) # 只比较数值
print(1.0==2)
print(1=='12') # 这点比matlab高级
logic1 = not [] # 空的认为是False
print('logic1:',logic1)
logic2 = not {}
print('logic2:',logic2) # 字典为空为逻辑False
logic3 = not {'12',12}
print('logic3:',logic3) # 字典不为空为逻辑True
#------------逻辑运算-----------#
logicA = [[True,False,True],[0,1,1]] # list也是可以运算的
logicB = [[False,True,True],[1,1,1]]
print('and:',logicA and logicB) # and
print('or:',logicA and logicB) # or
#------------逻辑索引-----------#
Array = array(random.randn(2,3,3))
print('Array=',Array,'\n','Array>0.5:\n',Array>0.5) # 将数组转化为logic list
运行结果:
True
False
False
logic1: True
logic2: True
logic3: False
and: [[False, True, True], [1, 1, 1]]
or: [[False, True, True], [1, 1, 1]]
Array= [[[-1.23763097 0.60445354 1.1511807 ]
[-0.16833538 1.84380922 0.97998012]
[-1.12324334 -0.74282353 1.52741753]]
[[ 0.34187249 0.05305085 1.74946176]
[-0.11840813 0.23020637 1.08800994]
[ 1.8168356 0.08452841 -0.79850746]]]
Array>0.5:
[[[False True True]
[False True True]
[False False True]]
[[False False True]
[False False True]
[ True False False]]]
2. 字符串运算
知识点:
- 字符串长度
- 子字符串查询
- 字符串连接
- 字符串分割
- 类型转换
程序如下:
#------------------string----------------#
s = "My hat"
type(s)
print(s[2:len(s)]) # len(str):获取字符串长度
#------substring-----#
print(s)
print('My' in s) # in
#------字符串连接-----#
strl1 = '%s%s' % (str(10),str(20)) # 方式一:推荐
strl2 = str(10)+str(20) # 方式二
#-------split-------#
string1 = '2017/6/8'
substrs = string1.split('/') # 单个分隔符'/'
print(substrs)
List = [float(i) for i in substrs]
print(List)
运行结果:
hat
My hat
True
['2017', '6', '8']
[2017.0, 6.0, 8.0]
3. 整型-浮点型-复数
知识点
- 用type()查看类型
- 基本复数运算
- 复数的共轭
- 使用numpy.array()创建复数矩阵,复数矩阵运算可以类比实数
程序如下:
# coding=utf-8
#-----------------整数和浮点数--------------#
num = 12 # 默认存为整数
num2 = 12.2
print(type(num),num)
print(type(num2),num2)
#--------------------复数------------------#
Z = complex(12,12) # Z2= 12+12j # 复数,j默认为复数单位
print(type(Z),Z,'real:',Z.real,'Image:',Z.imag) # 实部虚部
print('共轭:',Z.conjugate()) # 共轭
print('abs(Z)',abs(Z)) # 模值
Z_1 = 1/(1+1j) # 注意,必须是1j
print('1/(1+j)=',Z_1)
#----------常用的是矩阵形式---------#
import numpy as np
A = np.random.rand(5,5)+np.random.rand(5,5)*1j
print('real(A)=',A.real,'imag(A)=',A.imag)
运行结果:
<class 'int'> 12
<class 'float'> 12.2
<class 'complex'> (12+12j) real: 12.0 Image: 12.0
共轭: (12-12j)
abs(Z) 16.97056274847714
1/(1+j)= (0.5-0.5j)
real(A)= [[0.88264155 0.04743139 0.31278868 0.207474 0.70287479]
[0.82141479 0.94924559 0.02905332 0.40892528 0.86981736]
[0.11732918 0.30862477 0.13134238 0.60753618 0.08562458]
[0.21301151 0.98865014 0.90425279 0.23183489 0.45921353]
[0.18279321 0.21231794 0.6584834 0.28877172 0.16623646]] imag(A)= [[0.16415756 0.21810198 0.9844621 0.78848731 0.5329944 ]
[0.0739337 0.02073503 0.65747983 0.26515728 0.53676255]
[0.4647971 0.80725941 0.33550092 0.29294221 0.55686725]
[0.26336803 0.93787799 0.2792897 0.35741739 0.8114194 ]
[0.29019725 0.54495128 0.14364033 0.94534009 0.70025193]]
总结:总体来说并不难
版权声明:本文为博主原创文章,未经博主允许不得转载。