@guoxs
2016-03-06T08:17:15.000000Z
字数 6649
阅读 2741
python
python是一种解释型、面向对象,动态数据类型的高级程序语言。
//精度高,执行时间长
from decimal import Decimal as D
>>> type(a)
<type 'int'>
>>> a = "gh"
>>> type(a)
<type 'str'>
>>> a=[1,2,3,4]
>>> dir(a)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>> a.count(3)
1
>>> a = "dfgfht"
>>> a.count("d")
1
>>> a.__class__
<type 'str'>
>>> a.__doc__
"str(object='') -> string\n\nReturn a nice string representation of the object.\nIf the argument is a string, the return value is the same object."
>>> a = 3.4
>>> type(a)
<type 'float'>
>>> 0.3*3
0.8999999999999999
>>> print(0.3*3)
0.9
>>> 0.3/3
0.09999999999999999
>>> 10/3
3
>>> 10.0/3
3.3333333333333335
>>> from decimal import Decimal as D
>>> D(0.3)*D(0.9)
Decimal('0.2699999999999999966693309261')
$ python -mtimeit -s "from decimal import Decimal as D" "D('1.2')+D('3.4')"
10000 loops, best of 3: 28.5 usec per loop
$ python -mtimeit -s "from decimal import Decimal as D" "1.2+3.4"
100000000 loops, best of 3: 0.0172 usec per loop
$ python -mtimeit -s "from decimal import Decimal as D" "float('1.2')+float('3.4')"
1000000 loops, best of 3: 0.494 usec per loop
import math
//π
math.pi
//平方根
math.sqrt()
//对数
math.log10()
//x^y
math.pow(x,y)
//!x
math.factorial(x)
import random
//产生一个[0,1]的浮点数
random.random()
//从列表里等概率随机选择一个
random.choice([1,2,3,4])
//a-b之间整数随机取一个
random.randint(a,b)
//返回a~b之间均匀分布的随机数,浮点数
random.uniform(a,b)
//高斯分布,(均值,方差)
random.gauss(mu,lamda)
>>> import math
>>> math.factorial(5)
120
>>> math.pi
3.141592653589793
>>> dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
>>> help(math)
……
numpy
scipy
>>> s = "use python do something"
>>> s[1]
's'
>>> s[-1]
'g'
>>> s[1:3]
'se'
>>> s[1:6:2] //[start,stop,step]
's y'
>>> s[1:]
'se python do something'
>>> s[:-1]
'use python do somethin'
>>> s[:]
'use python do something'
常用方法集合
>>> "let us" + s,s*2
('let ususe python do something', 'use python do somethinguse python do something')
>>> s.upper()
'USE PYTHON DO SOMETHING'
>>> s.find("pa")
-1
>>> s.replace("python","java")
'use java do something'
>>> print "%s like %s" %("we","python")
we like python
>>> a = s.split(" ")
>>> a
['use', 'python', 'do', 'something']
>>> b = ", ".join(a)
>>> b
'use, python, do, something'
>>> s[0:4]*5
'use use use use use '
>>> s = " life is short, we use python! "
>>> a = s.split(",")
>>> a[1]
' we use python! '
>>> a[1].strip()
'we use python!'
//转义
>>> s="C:\newpython"
>>> print s
C:
ewpython
>>> len(s)
11
>>> s = r"C:\newpython"
>>> print s
C:\newpython
Regular expression
编译选项
import re
text = 'c++ python2 python3 perl ruby lua java javascript php4 php5 c'
#match,search,findall,split,sub
re.match(r'java',text) #none(第一个不是“java”)
re.search(r'java',text) #<_sre.SRE_Match object at 0x00000000031046B0>
re.match(r'c++',text) #error
re.match(r'c\+\+',text) #<_sre.SRE_Match object at 0x00000000031941D0>
print re.findall(r'python',text) #['python', 'python']
print re.split(r' perl ',text) #['c++ python2 python3', 'ruby lua java javascript php4 php5 c']
print re.sub(r'ruby','fortran',text) #c++ python2 python3 perl fortran lua java javascript php4 php5 c
# ^ start
# $ end
# . except \n
print re.findall(r'^c..',text) #['c++']
print re.findall(r'c+',text) #['c', 'c', 'c']
print re.findall(r'c\++',text) #['c++']
print re.findall(r'c$',text) #['c']
# + 1-inf
# * 0-inf
# ? 0-1,
# [] or
# {} repeat
# [^] not
print re.findall(r'p+',text)
#['p', 'p', 'p', 'p', 'p', 'p', 'p', 'p']
print re.findall(r'p[a-zA-Z]+',text) #{1,}
#['python', 'python', 'perl', 'pt', 'php', 'php']
print re.findall(r'p[a-zA-Z]*',text) #{0,}
#['python', 'python', 'perl', 'pt', 'php', 'php']
print re.findall(r'p[a-zA-Z]?',text) #{0,1}
#['py', 'py', 'pe', 'pt', 'ph', 'p', 'ph', 'p']
print re.findall(r'p[a-zA-Z0-9]{3,}',text) #{,1}
#['python2', 'python3', 'perl', 'php4', 'php5']
print re.findall(r'c[a-zA-Z]*',text) #{,inf}
#['c', 'cript', 'c']
print re.findall(r'c[^a-zA-Z]*',text) #{,inf}
#['c++ ', 'c', 'c']
# | or
print re.findall(r'[pj][a-zA-Z]+',text) #{,inf}
#['python', 'python', 'perl', 'java', 'javascript', 'php', 'php']
# |重写上面的pattern
print re.findall(r'p[^0-9]+|j[a-zA-Z]+',text)
#['python', 'python', 'perl ruby lua java javascript php', 'php']
print re.findall(r'p[^0-9 ]+|j[a-zA-Z]+',text)
#['python', 'python', 'perl', 'java', 'javascript', 'php', 'php']
# \w [a-zA-Z0-9_], \W
# \d [0-9], \D
# \s [ \t\n\r\f\v], \S
print re.findall(r'p\w+',text) #['python2', 'python3', 'perl', 'pt', 'php4', 'php5']
print re.findall(r'p\w+\d',text) #['python2', 'python3', 'php4', 'php5']
print re.findall(r'p\w+[0-9]',text) #['python2', 'python3', 'php4', 'php5']
print re.findall(r'p\w{5,9}',text) #['python2', 'python3']
# \b word boundary
# \B not \b
# \A input start, ^
# \Z input end, $
print re.findall(r'\bp[^0-9]',text) #['py', 'py', 'pe', 'ph', 'ph']
print re.findall(r'p[^0-9]\b',text) #['pt']
# *? 0~inf non-greedy
# +? 1~inf non-greedy
print re.findall(r'p[a-z]*',text) #['python', 'python', 'perl', 'pt', 'php', 'php']
print re.findall(r'p[a-z]*?',text) #['p', 'p', 'p', 'p', 'p', 'p', 'p', 'p']
print re.findall(r'p[a-z]+\b',text) #['perl', 'pt']
print re.findall(r'p[a-z]+?\b',text) #['perl', 'pt']
# () group
# (?P<name>pattern)
a=re.search(r'(p[a-zA-Z]+)([0-9])','python2',re.X)
print a.group(1) #python
print a.group(2) #2
a=re.search(r'(?P<name>p[a-zA-Z]+)(?P<version>[0-9])','python2')
print a.group('name') #python
print a.group('version') #2
print a.groupdict() #{'version': '2', 'name': 'python'}
pattern = re.compile(r'(?P<name>p[a-zA-Z]+)(?P<version>[0-9])')
results = pattern.search('python2')
print results.groupdict() #{'version': '2', 'name': 'python'}
results = pattern.search('python3')
print results.groupdict() #{'version': '3', 'name': 'python'}
results = pattern.search('php4')
print results.groupdict() #{'version': '4', 'name': 'php'}
#########################################
for t in text.split(' '):
results = pattern.search(t)
if results:
print results.groupdict()
#{'version': '2', 'name': 'python'}
#{'version': '3', 'name': 'python'}
#{'version': '4', 'name': 'php'}
#{'version': '5', 'name': 'php'}
###########################################
a = re.compile(r"""\d + # 整数部分
\. # 小数点
\d * # 小数部分
""", re.X)
b = re.compile(r"\d+\.\d*")