python学习笔记

常用函数

1. locals() 和 globals()

locals() 和 globals() 是python 的内建函数,分别提供了字典的形式访问局部变量和全局变量的方式。
这里的访问包括:读取、创建、修改。

#以locals()为例说明两个函数,globals()用法相同
#读取
locals()#读取所有本地变量
locals()['x']#读取变量名为x的变量
locals()['y']=10#如果y不存在则创建y并令y=10,如果存在则令y=10

2.lambda

lambda用来定义一个匿名函数,使用方法如下所示:
add = lambda x, y : x+y
add(1,2) # 结果为3

3.matlab和python调用

Matlab调用Python

使用IPDB调试程序

安装

  1. 可以使用anaconda,spyder集成有IPDB调试工具
  2. 使用pip install ipdb安装

调用

  1. 源代码中调用
    通过在代码开头导入包,可以直接在代码指定位置插入断点。如下所示:

    import ipdb
    x = "前面的程序"
    ipdb.set_trace()#调试点
    y = "后面的程序"
    

    说明:程序会在执行完ipdb.set_trace()"这条语句之后停止,进入调试。

  2. 命令行调用
    上面的方法很方便,但是也有不灵活的缺点。对于一段比较棘手的代码,我们可能需要按步执行,边运行边跟踪代码流并进行调试,这时候使用交互式的命令式调试方法更加有效。启动IPDB调试环境的方法也很简单:
    python -m ipdb your_code.py

命令

在IPDB环境中输入h获得支持的命令列表如下:
在这里插入图片描述
下面按照功能划分解释常用命令的作用,用⭐️标记命令的常用度,按照命令的常用度倒序排列。输入h 命令即可获得命令说明(下称官方解释)。

程序运行

  1. n/next ⭐️⭐️⭐️⭐️⭐️
    官方解释:n(ext)
    Continue execution until the next line in the current function is reached or it returns.
    本文解释:使用n(next)执行下一条语句。注意一个函数调用也是一个语句。进入函数内部的功能见s。
  2. s/step⭐️⭐️⭐️⭐️⭐️
    官方解释:Execute the current line, stop at the first possible occasion (either in a function that is called or in the current function).
  3. unt/until ⭐️⭐️⭐️⭐️⭐️
    官方解释:unt(il) [lineno]
    Without argument, continue execution until the line with a number greater than the current one is reached. With a line number, continue execution until a line with a number greater or equal to that is reached. In both cases, also stop when the current frame returns.
    本文解释:没有参数,继续执行直到行数大于目前的行数;把行数作为参数,继续执行到参数所在的行数。
  4. run/restart ⭐️⭐️⭐️⭐️
    官方解释:run [args…]
    Restart the debugged python program. If a string is supplied it is split with “shlex”, and the result is used as the new sys.argv. History, breakpoints, actions and debugger options are preserved. “restart” is an alias for “run”.
  5. q/quit ⭐️⭐️⭐️⭐️
    官方解释:Quit from the debugger. The program being executed is aborted.
  6. j/jump⭐️⭐️
    官方解释:j(ump) lineno
    Set the next line that will be executed. Only available in the bottom-most frame.This lets you jump back and execute code again, or jump forward to skip code that you don’t want to run.
    It should be noted that not all jumps are allowed – for instance it is not possible to jump into the middle of a for loop or out of a finally clause.

断点

  1. b/break ⭐️⭐️⭐️⭐️⭐️
    官方解释:b(reak) [ ([filename:]lineno | function) [, condition] ]
    Without argument, list all breaks. With a line number argument, set a break at this line in the current file. With a function name, set a break at the first executable line of that function. If a second argument is present, it is a string specifying an expression which must evaluate to true before the breakpoint is honored.The line number may be prefixed with a filename and a colon,to specify a breakpoint in another file (probably one that hasn’t been loaded yet). The file is searched for on sys.path; the .py suffix may be omitted.
  2. cl(ear) ⭐️⭐️⭐️⭐️⭐️
    官方解释:cl(ear) [bpnumber [bpnumber…]]
    With a space separated list of breakpoint numbers, clear those breakpoints. Without argument, clear all breaks (but first ask confirmation). With a filename:lineno argument,clear all breaks at that line in that file.
    本文解释:不带参数则清除所有断点,带参数(断点的序号1,2,3…)则清除指定断点。
  3. disable ⭐️⭐️⭐️
    官方解释:bpnumber [bpnumber …]
    Disables the breakpoints given as a space separated list of breakpoint numbers. Disabling a breakpoint means it cannot cause the program to stop execution, but unlike clearing a breakpoint, it remains in the list of breakpoints and can be (re-)enabled.
    本文解释:禁用指定的断点,被禁用的断点不会被删除,但程序运行会忽视。

查看数据

  1. p ⭐️⭐️⭐️⭐️⭐️
    官方解释:p expression
    Print the value of the expression.
  2. psource ⭐️⭐️
    官方解释:Print (or run through pager) the source code for an object.
  3. display ⭐️⭐️
    官方解释:display [expression]
    Display the value of the expression if it changed, each time execution stops in the current frame.Without expression, list all display expressions for the current frame.
  4. rv⭐️⭐️
    官方解释:Print the return value for the last return of a function.
  5. a(rgs) ⭐️⭐️⭐️
    官方解释:a(rgs) Print the argument list of the current function.

其它

  1. EOF ⭐️
    官方解释:Handles the receipt of EOF as a command.
  2. interact ⭐️
    官方解释: Start an interactive interpreter whose global namespace contains all the (global and local) names found in the current scope.

numpy

1.将多维数组展成一维

两种方式,分别为:

a=np.array([[0,1,2,3],
		   	[4,5,6,7]])
#1
a.flatten() #out:array([0, 1, 2, 3, 4, 5, 6, 7])
#2
a.ravel() #out:array([0, 1, 2, 3, 4, 5, 6, 7])

两者的区别:

a.flatten()[0]=8
print(a) 
#out:([[0,1,2,3],
#     [4,5,6,7]])
a.ravel()[0]=8
print(a) 
#out:([[8,1,2,3],
#	  [4,5,6,7]])

总结:a.ravel()数组进行操作会改变原来a数组中的值,对flatten()生成的数组操作不会改变原数组。

2.数组的遍历

使用的是numpy.nditer()这个方法,具体函数说明请见官方函数说明。

#使用如下方式也可以迭代,这种直接迭代,i是每一行的元素集合
arr=np.arrange(10).reshape(2,5)
for i in arr:
	print(i)

进一步说明以后再改,先放一个其他人写的

3.数组排序/找最大的k个数

1.用于找到最大的k个数

numpy.argpartition(a, kth, axis=-1, kind='introselect', order=None)

在快排算法中,有一个典型的操作:partition。这个操作指:根据一个数值x,把数组中的元素划分成两半,使得index前面的元素都不大于x,index后面的元素都不小于x。numpy中的argpartition()函数就是起的这个作用。对于传入的数组a,先用O(n)复杂度求出第k大的数字,然后利用这个第k大的数字将数组a划分成两半。此函数不对原数组进行操作,它只返回分区之后的下标。一般numpy中以arg开头的函数都是返回下标而不改变原数组。此函数还有另外两个参数:
kind:用于指定partition的算法
order:表示排序的key,也就是按哪些字段进行排序
当我们只关心topK时,我们不需要使用np.sort()对数组进行全量排序,np.argpartition()已经够用了。
官方API介绍

4.数组乘和矩阵乘(点乘)

“*”在numpy中是数组乘
".dot()"是矩阵乘法,即行列相乘相加
下面举例说明:

  1. 数组乘
a = numpy.array([
				[1,2],
                [3,4]
                ])
b = numpy.array([
				[5,6],
                [7,8]
                ])
a*b
>>>array([[ 5, 12],
          [21, 32]])
  1. 矩阵乘
a = numpy.array([
				[1,2],
                [3,4]
                ])
b = numpy.array([
				[5,6],
                [7,8]
                ])
#下面两种方式都可以
a.dot(b)#1
numpy.dot(a,b)#2

>>>array([[19, 22],
          [43, 50]])

5. 多维数组按轴运算(如按行取均值)

c = np.array([[1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, 10]])
print(c.mean(axis=1))#行
print(c.mean(axis=0))#列

>>>[ 2.5  5.5  8.5]
>>>[ 4.  5.  6.  7.]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值