分类: Python/Ruby
2016-06-11 23:10:00
--------------------------------------------------------------------------------------
li=[11,22]
type(li) ----- dir(list) -------help(list) ------ help(list.append)
数字:
__cmp__ 比较大小 1 、0 、-1
__add__ 加
__divmod__ 商和余数
--------------------------------------------------------------------------------------
字符串内置方法:
>>> name
'nginx'
>>> name.capitalize()
'Nginx'
>>> name.center(20,"*")
'*******nginx********'
>>> name.ljust(30,"*")
'nginx*************************'
>>> name.count("n")
2
>>> name.count("n",1,4)
1
>>> name.endswith("x")
True
>>> name.startswith("n")
True
>>> name.find("n")
0
>>> name.find("j")
-1
>>> name.index("n")
0
>>> name.index("j")
Traceback (most recent call last):
File "", line 1, in
ValueError: substring not found
>>> name.partition('n')
('', 'n', 'ginx')
>>> name.partition('i')
('ng', 'i', 'nx')
>>> name.replace("n","Tn")
'TngiTnx'
>>> name.replace("i","j")
'ngjnx'
>>> name.split("i")
['ng', 'nx']
>>> name1=" xiaofan "
>>> name1.strip()
'xiaofan'
>>> name*5
'nginxnginxnginxnginxnginx'
.isalnum() 是否字母数字
.islower() 是否小写
.isspace() 是否空格
.istitle() 是否标题
.title() 变成标题格式(首字母大写的)
.lower() 变成小写
.upper() 变成大写
.swapcase() 大写变小写,小写变大写
字符编码:
--------------------------------------------------------------------------------------
列表:
>>> name=["nginx","tomcat","php"]
>>> name.insert(0,"linux")
>>> name
['linux', 'nginx', 'tomcat', 'php']
>>> name.pop()
'php'
>>> name
['linux', 'nginx', 'tomcat']
>>> name.reverse()
>>> name
['tomcat', 'nginx', 'linux']
>>> name.sort()
>>> name
['linux', 'nginx', 'tomcat']
--------------------------------------------------------------------------------------
字典:
循环(数据量大时这个好)
>>> dic={"web":"apache","db":"mysql"}
>>> for k in dic:
... print k,dic[k]
...
web apache
db mysql
(数据量大时慢)
>>> for k,v in dic.items():
... print k
... print v
...
web
apache
db
mysql
更新字典:
>>> dic1={"name":"lvs","fun":"balance"}
>>> dic.update(dic1)
>>> dic
{'fun': 'balance', 'web': 'apache', 'db': 'mysql', 'name': 'lvs'}
浅拷贝(拷贝一层)
>>> f=dic.copy()
>>> f
{'fun': 'balance', 'web': 'apache', 'db': 'mysql', 'name': 'lvs'}
引用:
>>> ff=dic
>>> ff
{'fun': 'balance', 'web': 'apache', 'db': 'mysql', 'name': 'lvs'}
深拷贝(拷贝所以)
>>> import copy
>>> f1=copy.deepcopy(dic)
>>> f1
{'fun': 'balance', 'web': 'apache', 'db': 'mysql', 'name': 'lvs'}
>>> dic.keys()
['fun', 'web', 'db', 'name']
>>> dic.values()
['balance', 'apache', 'mysql', 'lvs']
--------------------------------------------------------------------------------------
集合:
>>> a = range(5,10)
>>> b = range(7,12)
>>> b
[7, 8, 9, 10, 11]
>>> d=set(b)
>>> d
set([8, 9, 10, 11, 7])
交集:
>>> c&d
set([8, 9, 7])
交集取反:
>>> c^d
set([5, 6, 10, 11])
并集:
>>> c|d
set([5, 6, 7, 8, 9, 10, 11])
c中有d中没有的:
>>> c-d
set([5, 6])
c是否的的子集:
>>> c.issubset(d)
False
c是否d的父集:
>>> c.issuperset(d)
False
删除一个元素:
>>> d.remove(11)
>>> d
set([8, 9, 10, 7])
--------------------------------------------------------------------------------------
计算:
>>> eval("40-7*4")
12