python中遇到的小问题

来源:互联网 发布:大数据平台和云平台 编辑:程序博客网 时间:2024/05/13 10:52
1.os.system
import osos.system('route xxx.xxx.xxx.xxx')

自己写的一个添加路由表项的小模块,文件名是route.py。

在文件所在目录执行route.py,出现死循环,百思不得其解。

后来发现os.system相当于cmd,优先调用本目录下的程序,于是以上语句调用了route.py,而不是route.exe。

改了文件名后成功。

注意细节,以为戒。

 

2.re.findall

>>> re.findall('([0-9]{1,3}/.){3}/d{1,3}', '0.1.2.3')['2.']>>> re.search('([0-9]{1,3}/.){3}/d{1,3}', '0.1.2.3').group(0)'0.1.2.3'

查了手册,在re的Match Objects的group函数解释中有这样一句话:

(If a group is contained in a part of the pattern that matched multiple times, the last match is returned. )

 

3.list

a[起始序号:终止序号:步长]

如果要逆序排列,只要a[::-1]即可。

如果作为pattern一部分的一个group匹配了多次,将返回最后一个匹配结果。

虽然不是关于findall的,但我想findall可能是调用了group函数返回的。

勉强可以解释,留待以后深究。现在只能先用

re.findall('/d{1,3}/./d{1,3}/./d{1,3}/./d{1,3}', '0.1.2.3')
4.默认参数只执行一次
def function(item, stuff=[]):    stuff.append(item)    print stufffunction(1)# [1]function(2)# [1,2]
因此,不要使用可变对象作为默认参数。
原创粉丝点击