Python基础 numpy中的常见函数有哪些 python 常用的系统函数有哪些

python\u4e2d\u51fd\u6570\u90fd\u6709\u54ea\u4e9b\u7b80\u5355\u70b9\u7684\u4f8b\u5b50\u96f6\u57fa\u7840

Python \u4e2d\uff0c\u51fd\u6570\u7684\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\uff0c\u6bd4\u5982 input() \u3001print()\u3001range()\u3001len() \u51fd\u6570\u7b49\u7b49\uff0c\u8fd9\u4e9b\u90fd\u662f Python \u7684\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002
\u9664\u4e86\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7684\u5185\u7f6e\u51fd\u6570\u5916\uff0cPython \u8fd8\u652f\u6301\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u5373\u5c06\u4e00\u6bb5\u6709\u89c4\u5f8b\u7684\u3001\u53ef\u91cd\u590d\u4f7f\u7528\u7684\u4ee3\u7801\u5b9a\u4e49\u6210\u51fd\u6570\uff0c\u4ece\u800c\u8fbe\u5230\u4e00\u6b21\u7f16\u5199\u3001\u591a\u6b21\u8c03\u7528\u7684\u76ee\u7684\u3002
\u901a\u5e38\uff0c\u51fd\u6570\u53ef\u4ee5\u63a5\u6536\u96f6\u4e2a\u6216\u591a\u4e2a\u53c2\u6570\uff0c\u4e5f\u53ef\u4ee5\u8fd4\u56de\u96f6\u4e2a\u6216\u591a\u4e2a\u503c\u3002\u4ece\u51fd\u6570\u4f7f\u7528\u8005\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u51fd\u6570\u5c31\u50cf\u4e00\u4e2a\u201c\u9ed1\u5323\u5b50\u201d\uff0c\u7a0b\u5e8f\u5c06\u96f6\u4e2a\u6216\u591a\u4e2a\u53c2\u6570\u4f20\u5165\u8fd9\u4e2a\u201c\u9ed1\u5323\u5b50\u201d\uff0c\u8be5\u201c\u9ed1\u5323\u5b50\u201d\u7ecf\u8fc7\u4e00\u756a\u8ba1\u7b97\u5373\u53ef\u8fd4\u56de\u96f6\u4e2a\u6216\u591a\u4e2a\u503c\u3002
\u4e0b\u9762\u7a0b\u5e8f\u5b9a\u4e49\u4e86\u4e24\u4e2a\u51fd\u6570\uff1a
\u7eaf\u6587\u672c\u590d\u5236
def my_max(x, y) :# \u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cfz\uff0c\u8be5\u53d8\u91cf\u7b49\u4e8ex\u3001y\u4e2d\u8f83\u5927\u7684\u503c
z = x if x > y else y# \u8fd4\u56de\u53d8\u91cfz\u7684\u503c
return z# \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u58f0\u660e\u4e00\u4e2a\u5f62\u53c2
def say_hi(name) :print("===\u6b63\u5728\u6267\u884csay_hi()\u51fd\u6570===")
return name + "\uff0c\u60a8\u597d\uff01"


\u8c03\u7528\u524d\u9762\u521b\u5efa\u7684\u90a3 2 \u4e2a\u51fd\u6570\uff0c\u6267\u884c\u4ee3\u7801\u5982\u4e0b\uff1a
a = 6

b = 9
# \u8c03\u7528my_max()\u51fd\u6570\uff0c\u5c06\u51fd\u6570\u8fd4\u56de\u503c\u8d4b\u503c\u7ed9result\u53d8\u91cf
result = my_max(a , b) # \u2460
print("result:", result)
# \u8c03\u7528say_hi()\u51fd\u6570\uff0c\u76f4\u63a5\u8f93\u51fa\u51fd\u6570\u7684\u8fd4\u56de\u503c
print(say_hi("\u5b59\u609f\u7a7a")) # \u2461

\u4e0a\u9762\u7a0b\u5e8f\u4e2d\uff0c\u5206\u522b\u5728 \u2460 \u53f7\u3001\u2461 \u53f7\u4ee3\u7801\u5904\u8c03\u7528\u4e86 my_max() \u548c say_hi() \u8fd9\u4e24\u4e2a\u51fd\u6570\u3002\u4ece\u4e0b\u9762\u7684\u8fd0\u884c\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff0c\u5f53\u7a0b\u5e8f\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u65e2\u53ef\u4ee5\u628a\u8c03\u7528\u51fd\u6570\u7684\u8fd4\u56de\u503c\u8d4b\u503c\u7ed9\u6307\u5b9a\u53d8\u91cf\uff0c\u4e5f\u53ef\u4ee5\u5c06\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4f20\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u53c2\u6570\u3002
\u8fd0\u884c\u4e0a\u9762\u7a0b\u5e8f\uff0c\u5c06\u53ef\u4ee5\u770b\u5230\u5982\u4e0b\u8fd0\u884c\u7ed3\u679c\uff1a

result: 9
===\u6b63\u5728\u6267\u884csay_hi()\u51fd\u6570===
\u5b59\u609f\u7a7a\uff0c\u60a8\u597d\uff01

1.\u5e38\u7528\u5185\u7f6e\u51fd\u6570\uff1a(\u4e0d\u7528import\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528)
help(obj) \u5728\u7ebf\u5e2e\u52a9, obj\u53ef\u662f\u4efb\u4f55\u7c7b\u578b
callable(obj) \u67e5\u770b\u4e00\u4e2aobj\u662f\u4e0d\u662f\u53ef\u4ee5\u50cf\u51fd\u6570\u4e00\u6837\u8c03\u7528
repr(obj) \u5f97\u5230obj\u7684\u8868\u793a\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u5229\u7528\u8fd9\u4e2a\u5b57\u7b26\u4e32eval\u91cd\u5efa\u8be5\u5bf9\u8c61\u7684\u4e00\u4e2a\u62f7\u8d1d
eval_r(str) \u8868\u793a\u5408\u6cd5\u7684python\u8868\u8fbe\u5f0f\uff0c\u8fd4\u56de\u8fd9\u4e2a\u8868\u8fbe\u5f0f
dir(obj) \u67e5\u770bobj\u7684name space\u4e2d\u53ef\u89c1\u7684name
hasattr(obj,name) \u67e5\u770b\u4e00\u4e2aobj\u7684name space\u4e2d\u662f\u5426\u6709name
getattr(obj,name) \u5f97\u5230\u4e00\u4e2aobj\u7684name space\u4e2d\u7684\u4e00\u4e2aname
setattr(obj,name,value) \u4e3a\u4e00\u4e2aobj\u7684name space\u4e2d\u7684\u4e00\u4e2aname\u6307\u5411vale\u8fd9\u4e2aobject
delattr(obj,name) \u4eceobj\u7684name space\u4e2d\u5220\u9664\u4e00\u4e2aname
vars(obj) \u8fd4\u56de\u4e00\u4e2aobject\u7684name space\u3002\u7528dictionary\u8868\u793a
locals() \u8fd4\u56de\u4e00\u4e2a\u5c40\u90e8name space,\u7528dictionary\u8868\u793a
globals() \u8fd4\u56de\u4e00\u4e2a\u5168\u5c40name space,\u7528dictionary\u8868\u793a
type(obj) \u67e5\u770b\u4e00\u4e2aobj\u7684\u7c7b\u578b
isinstance(obj,cls) \u67e5\u770bobj\u662f\u4e0d\u662fcls\u7684instance
issubclass(subcls,supcls) \u67e5\u770bsubcls\u662f\u4e0d\u662fsupcls\u7684\u5b50\u7c7b

\u7c7b\u578b\u8f6c\u6362\u51fd\u6570
chr(i) \u628a\u4e00\u4e2aASCII\u6570\u503c,\u53d8\u6210\u5b57\u7b26
ord(i) \u628a\u4e00\u4e2a\u5b57\u7b26\u6216\u8005unicode\u5b57\u7b26,\u53d8\u6210ASCII\u6570\u503c
oct(x) \u628a\u6574\u6570x\u53d8\u6210\u516b\u8fdb\u5236\u8868\u793a\u7684\u5b57\u7b26\u4e32
hex(x) \u628a\u6574\u6570x\u53d8\u6210\u5341\u516d\u8fdb\u5236\u8868\u793a\u7684\u5b57\u7b26\u4e32
str(obj) \u5f97\u5230obj\u7684\u5b57\u7b26\u4e32\u63cf\u8ff0
list(seq) \u628a\u4e00\u4e2asequence\u8f6c\u6362\u6210\u4e00\u4e2alist
tuple(seq) \u628a\u4e00\u4e2asequence\u8f6c\u6362\u6210\u4e00\u4e2atuple
dict(),dict(list) \u8f6c\u6362\u6210\u4e00\u4e2adictionary
int(x) \u8f6c\u6362\u6210\u4e00\u4e2ainteger
long(x) \u8f6c\u6362\u6210\u4e00\u4e2along interger
float(x) \u8f6c\u6362\u6210\u4e00\u4e2a\u6d6e\u70b9\u6570
complex(x) \u8f6c\u6362\u6210\u590d\u6570
max(...) \u6c42\u6700\u5927\u503c
min(...) \u6c42\u6700\u5c0f\u503c
\u7528\u4e8e\u6267\u884c\u7a0b\u5e8f\u7684\u5185\u7f6e\u51fd\u6570
complie \u5982\u679c\u4e00\u6bb5\u4ee3\u7801\u7ecf\u5e38\u8981\u4f7f\u7528,\u90a3\u4e48\u5148\u7f16\u8bd1,\u518d\u8fd0\u884c\u4f1a\u66f4\u5feb\u3002

2.\u548c\u64cd\u4f5c\u7cfb\u7edf\u76f8\u5173\u7684\u8c03\u7528
\u7cfb\u7edf\u76f8\u5173\u7684\u4fe1\u606f\u6a21\u5757 import sys
sys.argv\u662f\u4e00\u4e2alist,\u5305\u542b\u6240\u6709\u7684\u547d\u4ee4\u884c\u53c2\u6570.
sys.stdout sys.stdin sys.stderr \u5206\u522b\u8868\u793a\u6807\u51c6\u8f93\u5165\u8f93\u51fa,\u9519\u8bef\u8f93\u51fa\u7684\u6587\u4ef6\u5bf9\u8c61.
sys.stdin.readline() \u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u4e00\u884c sys.stdout.write("a") \u5c4f\u5e55\u8f93\u51faa
sys.exit(exit_code) \u9000\u51fa\u7a0b\u5e8f
sys.modules \u662f\u4e00\u4e2adictionary\uff0c\u8868\u793a\u7cfb\u7edf\u4e2d\u6240\u6709\u53ef\u7528\u7684module
sys.platform \u5f97\u5230\u8fd0\u884c\u7684\u64cd\u4f5c\u7cfb\u7edf\u73af\u5883
sys.path \u662f\u4e00\u4e2alist,\u6307\u660e\u6240\u6709\u67e5\u627emodule\uff0cpackage\u7684\u8def\u5f84.

\u64cd\u4f5c\u7cfb\u7edf\u76f8\u5173\u7684\u8c03\u7528\u548c\u64cd\u4f5c import os
os.environ \u4e00\u4e2adictionary \u5305\u542b\u73af\u5883\u53d8\u91cf\u7684\u6620\u5c04\u5173\u7cfb os.environ["HOME"] \u53ef\u4ee5\u5f97\u5230\u73af\u5883\u53d8\u91cfHOME\u7684\u503c
os.chdir(dir) \u6539\u53d8\u5f53\u524d\u76ee\u5f55 os.chdir('d:\\outlook') \u6ce8\u610fwindows\u4e0b\u7528\u5230\u8f6c\u4e49
os.getcwd() \u5f97\u5230\u5f53\u524d\u76ee\u5f55
os.getegid() \u5f97\u5230\u6709\u6548\u7ec4id os.getgid() \u5f97\u5230\u7ec4id
os.getuid() \u5f97\u5230\u7528\u6237id os.geteuid() \u5f97\u5230\u6709\u6548\u7528\u6237id
os.setegid os.setegid() os.seteuid() os.setuid()
os.getgruops() \u5f97\u5230\u7528\u6237\u7ec4\u540d\u79f0\u5217\u8868
os.getlogin() \u5f97\u5230\u7528\u6237\u767b\u5f55\u540d\u79f0
os.getenv \u5f97\u5230\u73af\u5883\u53d8\u91cf
os.putenv \u8bbe\u7f6e\u73af\u5883\u53d8\u91cf
os.umask \u8bbe\u7f6eumask
os.system(cmd) \u5229\u7528\u7cfb\u7edf\u8c03\u7528\uff0c\u8fd0\u884ccmd\u547d\u4ee4
\u64cd\u4f5c\u4e3e\u4f8b\uff1a
os.mkdir('/tmp/xx') os.system("echo 'hello' > /tmp/xx/a.txt") os.listdir('/tmp/xx')
os.rename('/tmp/xx/a.txt','/tmp/xx/b.txt') os.remove('/tmp/xx/b.txt') os.rmdir('/tmp/xx')
\u7528python\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684shell
#!/usr/bin/python
import os, sys
cmd = sys.stdin.readline()
while cmd:
os.system(cmd)
cmd = sys.stdin.readline()

\u7528os.path\u7f16\u5199\u5e73\u53f0\u65e0\u5173\u7684\u7a0b\u5e8f
os.path.abspath("1.txt") == os.path.join(os.getcwd(), "1.txt")
os.path.split(os.getcwd()) \u7528\u4e8e\u5206\u5f00\u4e00\u4e2a\u76ee\u5f55\u540d\u79f0\u4e2d\u7684\u76ee\u5f55\u90e8\u5206\u548c\u6587\u4ef6\u540d\u79f0\u90e8\u5206\u3002
os.path.join(os.getcwd(), os.pardir, 'a', 'a.doc') \u5168\u6210\u8def\u5f84\u540d\u79f0.
os.pardir \u8868\u793a\u5f53\u524d\u5e73\u53f0\u4e0b\u4e0a\u4e00\u7ea7\u76ee\u5f55\u7684\u5b57\u7b26 ..
os.path.getctime("/root/1.txt") \u8fd4\u56de1.txt\u7684ctime(\u521b\u5efa\u65f6\u95f4)\u65f6\u95f4\u6233
os.path.exists(os.getcwd()) \u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728
os.path.expanduser('~/dir') \u628a~\u6269\u5c55\u6210\u7528\u6237\u6839\u76ee\u5f55
os.path.expandvars('$PATH') \u6269\u5c55\u73af\u5883\u53d8\u91cfPATH
os.path.isfile(os.getcwd()) \u5224\u65ad\u662f\u5426\u662f\u6587\u4ef6\u540d\uff0c1\u662f0\u5426
os.path.isdir('c:\Python26\temp') \u5224\u65ad\u662f\u5426\u662f\u76ee\u5f55,1\u662f0\u5426
os.path.islink('/home/huaying/111.sql') \u662f\u5426\u662f\u7b26\u53f7\u8fde\u63a5 windows\u4e0b\u4e0d\u53ef\u7528
os.path.ismout(os.getcwd()) \u662f\u5426\u662f\u6587\u4ef6\u7cfb\u7edf\u5b89\u88c5\u70b9 windows\u4e0b\u4e0d\u53ef\u7528
os.path.samefile(os.getcwd(), '/home/huaying') \u770b\u770b\u4e24\u4e2a\u6587\u4ef6\u540d\u662f\u4e0d\u662f\u6307\u7684\u662f\u540c\u4e00\u4e2a\u6587\u4ef6
os.path.walk('/home/huaying', test_fun, "a.c")
\u904d\u5386/home/huaying\u4e0b\u6240\u6709\u5b50\u76ee\u5f55\u5305\u62ec\u672c\u76ee\u5f55,\u5bf9\u4e8e\u6bcf\u4e2a\u76ee\u5f55\u90fd\u4f1a\u8c03\u7528\u51fd\u6570test_fun.
\u4f8b\uff1a\u5728\u67d0\u4e2a\u76ee\u5f55\u4e2d\uff0c\u548c\u4ed6\u6240\u6709\u7684\u5b50\u76ee\u5f55\u4e2d\u67e5\u627e\u540d\u79f0\u662fa.c\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u3002
def test_fun(filename, dirname, names): //filename\u5373\u662fwalk\u4e2d\u7684a.c dirname\u662f\u8bbf\u95ee\u7684\u76ee\u5f55\u540d\u79f0
if filename in names: //names\u662f\u4e00\u4e2alist,\u5305\u542bdirname\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u5185\u5bb9
print os.path.join(dirname, filename)
os.path.walk('/home/huaying', test_fun, "a.c")

\u6587\u4ef6\u64cd\u4f5c
\u6253\u5f00\u6587\u4ef6
f = open("filename", "r") r\u53ea\u8bfb w\u5199 rw\u8bfb\u5199 rb\u8bfb\u4e8c\u8fdb\u5236 wb\u5199\u4e8c\u8fdb\u5236 w+\u5199\u8ffd\u52a0
\u8bfb\u5199\u6587\u4ef6
f.write("a") f.write(str) \u5199\u4e00\u5b57\u7b26\u4e32 f.writeline() f.readlines() \u4e0e\u4e0bread\u7c7b\u540c
f.read() \u5168\u8bfb\u51fa\u6765 f.read(size) \u8868\u793a\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6size\u4e2a\u5b57\u7b26
f.readline() \u8bfb\u4e00\u884c,\u5230\u6587\u4ef6\u7ed3\u5c3e,\u8fd4\u56de\u7a7a\u4e32. f.readlines() \u8bfb\u53d6\u5168\u90e8\uff0c\u8fd4\u56de\u4e00\u4e2alist. list\u6bcf\u4e2a\u5143\u7d20\u8868\u793a\u4e00\u884c\uff0c\u5305\u542b"\n"\
f.tell() \u8fd4\u56de\u5f53\u524d\u6587\u4ef6\u8bfb\u53d6\u4f4d\u7f6e
f.seek(off, where) \u5b9a\u4f4d\u6587\u4ef6\u8bfb\u5199\u4f4d\u7f6e. off\u8868\u793a\u504f\u79fb\u91cf\uff0c\u6b63\u6570\u5411\u6587\u4ef6\u5c3e\u79fb\u52a8\uff0c\u8d1f\u6570\u8868\u793a\u5411\u5f00\u5934\u79fb\u52a8\u3002
where\u4e3a0\u8868\u793a\u4ece\u5f00\u59cb\u7b97\u8d77,1\u8868\u793a\u4ece\u5f53\u524d\u4f4d\u7f6e\u7b97,2\u8868\u793a\u4ece\u7ed3\u5c3e\u7b97.
f.flush() \u5237\u65b0\u7f13\u5b58
\u5173\u95ed\u6587\u4ef6
f.close()

regular expression \u6b63\u5219\u8868\u8fbe\u5f0f import re
\u7b80\u5355\u7684regexp
p = re.compile("abc") if p.match("abc") : print "match"
\u4e0a\u4f8b\u4e2d\u9996\u5148\u751f\u6210\u4e00\u4e2apattern(\u6a21\u5f0f),\u5982\u679c\u548c\u67d0\u4e2a\u5b57\u7b26\u4e32\u5339\u914d\uff0c\u5c31\u8fd4\u56de\u4e00\u4e2amatch object
\u9664\u67d0\u4e9b\u7279\u6b8a\u5b57\u7b26metacharacter\u5143\u5b57\u7b26\uff0c\u5927\u591a\u6570\u5b57\u7b26\u90fd\u548c\u81ea\u8eab\u5339\u914d\u3002
\u8fd9\u4e9b\u7279\u6b8a\u5b57\u7b26\u662f \u3002^ $ * + ? { [ ] \ | ( )
\u5b57\u7b26\u96c6\u5408(\u7528[]\u8868\u793a)
\u5217\u51fa\u5b57\u7b26,\u5982[abc]\u8868\u793a\u5339\u914da\u6216b\u6216c,\u5927\u591a\u6570metacharacter\u5728[]\u4e2d\u53ea\u8868\u793a\u548c\u672c\u8eab\u5339\u914d\u3002\u4f8b\uff1a
a = ".^$*+?{\\|()" \u5927\u591a\u6570metachar\u5728[]\u4e2d\u90fd\u548c\u672c\u8eab\u5339\u914d\uff0c\u4f46"^[]\"\u4e0d\u540c
p = re.compile("["+a+"]")
for i in a:
if p.match(i):
print "[%s] is match" %i
else:
print "[%s] is not match" %i
\u5728[]\u4e2d\u5305\u542b[]\u672c\u8eab\uff0c\u8868\u793a"["\u6216\u8005"]"\u5339\u914d.\u7528
\u548c
\u8868\u793a.
^\u51fa\u73b0\u5728[]\u7684\u5f00\u5934,\u8868\u793a\u53d6\u53cd.[^abc]\u8868\u793a\u9664\u4e86a,b,c\u4e4b\u5916\u7684\u6240\u6709\u5b57\u7b26\u3002^\u6ca1\u6709\u51fa\u73b0\u5728\u5f00\u5934\uff0c\u5373\u4e8e\u8eab\u8eab\u5339\u914d\u3002
-\u53ef\u8868\u793a\u8303\u56f4.[a-zA-Z]\u5339\u914d\u4efb\u4f55\u4e00\u4e2a\u82f1\u6587\u5b57\u6bcd\u3002[0-9]\u5339\u914d\u4efb\u4f55\u6570\u5b57\u3002
\\u5728[]\u4e2d\u7684\u5999\u7528\u3002
\d [0-9]
\D [^0-9]
\s [ \t\n\r\f\v]
\S [^ \t\n\r\f\v]
\w [a-zA-Z0-9_]
\W [^a-zA-Z0-9_]
\t \u8868\u793a\u548ctab\u5339\u914d, \u5176\u4ed6\u7684\u90fd\u548c\u5b57\u7b26\u4e32\u7684\u8868\u793a\u6cd5\u4e00\u81f4
\x20 \u8868\u793a\u548c\u5341\u516d\u8fdb\u5236ascii 0x20\u5339\u914d
\u6709\u4e86\\uff0c\u53ef\u4ee5\u5728[]\u4e2d\u8868\u793a\u4efb\u4f55\u5b57\u7b26\u3002\u6ce8\uff1a\u5355\u72ec\u7684\u4e00\u4e2a"."\u5982\u679c\u6ca1\u6709\u51fa\u73b0[]\u4e2d\uff0c\u8868\u793a\u51fa\u4e86\u6362\u884c\n\u4ee5\u5916\u7684\u5339\u914d\u4efb\u4f55\u5b57\u7b26,\u7c7b\u4f3c[^\n].
regexp\u7684\u91cd\u590d
{m,n}\u8868\u793a\u51fa\u73b0m\u4e2a\u4ee5\u4e0a(\u542bm\u4e2a),n\u4e2a\u4ee5\u4e0b(\u542bn\u4e2a). \u5982ab{1,3}c\u548cabc,abbc,abbbc\u5339\u914d\uff0c\u4e0d\u4f1a\u4e0eac,abbbc\u5339\u914d\u3002
m\u662f\u4e0b\u754c\uff0cn\u662f\u4e0a\u754c\u3002m\u7701\u7565\u8868\u4e0b\u754c\u662f0,n\u7701\u7565\uff0c\u8868\u4e0a\u754c\u65e0\u9650\u5927\u3002
*\u8868\u793a{,} +\u8868\u793a{1,} ?\u8868\u793a{0,1}
\u6700\u5927\u5339\u914d\u548c\u6700\u5c0f\u5339\u914d python\u90fd\u662f\u6700\u5927\u5339\u914d\uff0c\u5982\u679c\u8981\u6700\u5c0f\u5339\u914d\uff0c\u5728*,+,?,{m,n}\u540e\u9762\u52a0\u4e00\u4e2a?.
match object\u7684end\u53ef\u4ee5\u5f97\u5230\u5339\u914d\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u7684\u4f4d\u7f6e\u3002
re.compile("a*").match('aaaa').end() 4 \u6700\u5927\u5339\u914d
re.compile("a*?").match('aaaa').end() 0 \u6700\u5c0f\u5339\u914d
\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32
\u5b57\u7b26\u4e32\u8868\u793a\u65b9\u6cd5\u4e2d\u7528\\\u8868\u793a\u5b57\u7b26\.\u5927\u91cf\u4f7f\u7528\u5f71\u54cd\u53ef\u8bfb\u6027\u3002
\u89e3\u51b3\u65b9\u6cd5\uff1a\u5728\u5b57\u7b26\u4e32\u524d\u9762\u52a0\u4e00\u4e2ar\u8868\u793araw\u683c\u5f0f\u3002
a = r"\a" print a \u7ed3\u679c\u662f\a
a = r"\"a" print a \u7ed3\u679c\u662f\"a
\u4f7f\u7528re\u6a21\u5757
\u5148\u7528re.compile\u5f97\u5230\u4e00\u4e2aRegexObject \u8868\u793a\u4e00\u4e2aregexp
\u540e\u7528pattern\u7684match,search\u7684\u65b9\u6cd5,\u5f97\u5230MatchObject
\u518d\u7528match object\u5f97\u5230\u5339\u914d\u7684\u4f4d\u7f6e,\u5339\u914d\u7684\u5b57\u7b26\u4e32\u7b49\u4fe1\u606f
RegxObject\u5e38\u7528\u51fd\u6570:
>>> re.compile("a").match("abab") \u5982\u679cabab\u7684\u5f00\u5934\u548cre.compile("a")\u5339\u914d\uff0c\u5f97\u5230MatchObject

>>> print re.compile("a").match("bbab")
None \u6ce8\uff1a\u4ecestr\u7684\u5f00\u5934\u5f00\u59cb\u5339\u914d
>>> re.compile("a").search("abab") \u5728abab\u4e2d\u641c\u7d22\u7b2c\u4e00\u4e2a\u548cre_obj\u5339\u914d\u7684\u90e8\u5206

>>> print re.compile("a").search("bbab")
\u548cmatch()\u4e0d\u540c,\u4e0d\u5fc5\u4ece\u5f00\u5934\u5339\u914d
re_obj.findall(str) \u8fd4\u56destr\u4e2d\u641c\u7d22\u6240\u6709\u548cre_obj\u5339\u914d\u7684\u90e8\u5206.
\u8fd4\u56de\u4e00\u4e2atuple,\u5176\u4e2d\u5143\u7d20\u662f\u5339\u914d\u7684\u5b57\u7b26\u4e32.
MatchObject\u7684\u5e38\u7528\u51fd\u6570
m.start() \u8fd4\u56de\u8d77\u59cb\u4f4d\u7f6e,m.end()\u8fd4\u56de\u7ed3\u675f\u4f4d\u7f6e(\u4e0d\u5305\u542b\u8be5\u4f4d\u7f6e\u7684\u5b57\u7b26).
m.span() \u8fd4\u56de\u4e00\u4e2atuple\u8868\u793a(m.start(), m.end())
m.pos(), m.endpos(), m.re(), m.string()
m.re().search(m.string(), m.pos(), m.endpos()) \u4f1a\u5f97\u5230m\u672c\u8eab
m.finditer()\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2aiterator,\u7528\u6765\u904d\u5386\u6240\u6709\u627e\u5230\u7684MatchObject.
for m in re.compile("[ab]").finditer("tatbxaxb"):
print m.span()
\u9ad8\u7ea7regexp
| \u8868\u793a\u8054\u5408\u591a\u4e2aregexp. A B\u4e24\u4e2aregexp\uff0cA|B\u8868\u793a\u548cA\u5339\u914d\u6216\u8005\u8ddfB\u5339\u914d.
^ \u8868\u793a\u53ea\u5339\u914d\u4e00\u884c\u7684\u5f00\u59cb\u884c\u9996,^\u53ea\u6709\u5728\u5f00\u5934\u624d\u6709\u6b64\u7279\u6b8a\u610f\u4e49\u3002
$ \u8868\u793a\u53ea\u5339\u914d\u4e00\u884c\u7684\u7ed3\u5c3e
\A \u8868\u793a\u53ea\u5339\u914d\u7b2c\u4e00\u884c\u5b57\u7b26\u4e32\u7684\u5f00\u5934 ^\u5339\u914d\u6bcf\u4e00\u884c\u7684\u884c\u9996
\Z \u8868\u793a\u53ea\u5339\u914d\u884c\u4e00\u884c\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e $\u5339\u914d\u7b2c\u4e00\u884c\u7684\u884c\u5c3e
\b \u53ea\u5339\u914d\u8bcd\u7684\u8fb9\u754c \u4f8b\uff1a\binfo\b \u53ea\u4f1a\u5339\u914d"info" \u4e0d\u4f1a\u5339\u914dinformation
\B \u8868\u793a\u5339\u914d\u975e\u5355\u8bcd\u8fb9\u754c
\u793a\u4f8b\u5982\u4e0b\uff1a
>>> print re.compile(r"\binfo\b").match("info ") #\u4f7f\u7528raw\u683c\u5f0f \b\u8868\u793a\u5355\u8bcd\u8fb9\u754c

>>> print re.compile("\binfo\b").match("info ") #\u6ca1\u6709\u4f7f\u7528raw \b\u8868\u793a\u9000\u683c\u7b26\u53f7
None
>>> print re.compile("\binfo\b").match("\binfo\b ")

\u5206\u7ec4(Group) \u793a\u4f8b\uff1are.compile("(a(b)c)d").match("abcd").groups() ('abc', 'b')
#!/usr/local/bin/python
import re
x = """
name: Charles
Address: BUPT

name: Ann
Address: BUPT
"""
#p = re.compile(r"^name:(.*)\n^Address:(.*)\n", re.M)
p = re.compile(r"^name:(?P.*)\n^Address:(?P.*)\n", re.M)
for m in p.finditer(x):
print m.span()
print "here is your friends list"
print "%s, %s"%m.groups()
Compile Flag
\u7528re.compile\u5f97\u5230RegxObject\u65f6\uff0c\u53ef\u4ee5\u6709\u4e00\u4e9bflag\u7528\u6765\u8c03\u6574RegxObject\u7684\u8be6\u7ec6\u7279\u5f81.
DOTALL, S \u8ba9.\u5339\u914d\u4efb\u610f\u5b57\u7b26,\u5305\u62ec\u6362\u884c\u7b26\n
IGNORECASE, I \u5ffd\u7565\u5927\u5c0f\u5199
LOCALES, L \u8ba9\w \W \b \B\u548c\u5f53\u524d\u7684locale\u4e00\u81f4
MULTILINE, M \u591a\u884c\u6a21\u5f0f\uff0c\u53ea\u5f71\u54cd^\u548c$(\u53c2\u89c1\u4e0a\u4f8b)
VERBOSE, X verbose\u6a21\u5f0f

有些Python小白对numpy中的常见函数不太了解,今天小编就整理出来分享给大家。

Numpy是Python的一个科学计算的库,提供了矩阵运算的功能,其一般与Scipy、matplotlib一起使用。其实,list已经提供了类似于矩阵的表示形式,不过numpy为我们提供了更多的函数。

数组常用函数
1.where()按条件返回数组的索引值
2.take(a,index)从数组a中按照索引index取值
3.linspace(a,b,N)返回一个在(a,b)范围内均匀分布的数组,元素个数为N个
4.a.fill()将数组的所有元素以指定的值填充
5.diff(a)返回数组a相邻元素的差值构成的数组
6.sign(a)返回数组a的每个元素的正负符号
7.piecewise(a,[condlist],[funclist])数组a根据布尔型条件condlist返回对应元素结果
8.a.argmax(),a.argmin()返回a最大、最小元素的索引

改变数组维度
a.ravel(),a.flatten():将数组a展平成一维数组
a.shape=(m,n),a.reshape(m,n):将数组a转换成m*n维数组
a.transpose,a.T转置数组a

数组组合
1.hstack((a,b)),concatenate((a,b),axis=1)将数组a,b沿水平方向组合
2.vstack((a,b)),concatenate((a,b),axis=0)将数组a,b沿竖直方向组合
3.row_stack((a,b))将数组a,b按行方向组合
4.column_stack((a,b))将数组a,b按列方向组合

数组分割
1.split(a,n,axis=0),vsplit(a,n)将数组a沿垂直方向分割成n个数组
2.split(a,n,axis=1),hsplit(a,n)将数组a沿水平方向分割成n个数组

数组修剪和压缩
1.a.clip(m,n)设置数组a的范围为(m,n),数组中大于n的元素设定为n,小于m的元素设定为m
2.a.compress()返回根据给定条件筛选后的数组

数组属性
1.a.dtype数组a的数据类型
2.a.shape数组a的维度
3.a.ndim数组a的维数
4.a.size数组a所含元素的总个数
5.a.itemsize数组a的元素在内存中所占的字节数
6.a.nbytes整个数组a所占的内存空间7.a.astype(int)转换a数组的类型为int型

数组计算
1.average(a,weights=v)对数组a以权重v进行加权平均
2.mean(a),max(a),min(a),middle(a),var(a),std(a)数组a的均值、最大值、最小值、中位数、方差、标准差
3.a.prod()数组a的所有元素的乘积
4.a.cumprod()数组a的元素的累积乘积
5.cov(a,b),corrcoef(a,b)数组a和b的协方差、相关系数
6.a.diagonal()查看矩阵a对角线上的元素7.a.trace()计算矩阵a的迹,即对角线元素之和

以上就是numpy中的常见函数。更多Python学习推荐:PyThon学习网教学中心。

扩展阅读:python培训班学费一般多少 ... 免费的python编程软件 ... python初学编程必背 ... python scipy ... 学python后到底能干什么 ... jupyter notebook ... python编程入门自学 ... python在线编程入口 ... python基础代码大全 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网