Second, the data type of common methods

Everything in 1.python objects
2.type view an object's type
All functions 3.dir (type name) provided in the view class
4.help (type name) to view detailed functional class
5.help (type name, function name) to view the details of a feature class
 
There are two methods which class: underlined called built-in method, with no non-invasive method is called underlined
 
int type
1. Return absolute value of x
x.__abs__()《=========》abs(x)
2. The two numbers together
x .__ add __ (y) <=======> x + y value
3. The number of phase two
x.__and__(y)<=========>x&y的值
4. Comparison of two operand size
x .__ cmp __ (y) <========> cmp (x, y) <===========> x is less than y, return -1; x greater than y, return 1, are equal, 0;
The force generates a tuple
x.__coerce__(y)<=======>coerce(x,y)
6. division, get tuple quotient and the remainder consisting of
x.__divmod__(y)<====>divmod(x,y)
7. division, the quotient
x.__div__(y)<======>x/y 5.0/2=2.5
8. Conversion of floating type
x.__float__()<======>float(x)
9. In addition to the floor
x .__ floordiv __ (y) <====> x // y 5.0 // 2 = 2.0 (the same integer and divided)
10. spaces
x .__ format __ ( "10") "===" in front of the value of x with 10 spaces
 
 
String
1. String Format
name = 'I am {0},age {1}' name.format('rooney',28)
name = 'I am {ss},age {dd}' name.format(ss='rooney',age='28')
 
2.字符串格式化(放入列表)
li=['ramos',32](元组同理)
name = 'I am {0},age {1}' name.format(*li) ========》将列表li的值依次放入name中
 
dic = {'ss':123,'dd':456}
name = 'I am {ss},age {dd}' name.format(**dic) ========>将字典dic的值放入name中,键值与name的占位字符相对应
 
3.返回下标,find
name = 'rooney'
>>> name.find('e')
4
>>> name.find('o',0,2)
1
>>> name.find('s')
-1
 
总结:找到则返回下标,没有找到则返回-1
 
4.返回下标,index,find没有找到返回-1,index没有找到会报错。
>>> name.index('o')
1
>>> name.index('e')
4
>>> name.index('s')
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
name.index('s')
ValueError: substring not found
 
5.是否是字母和数字
>>> name = "rooney123"
>>> name.isalnum()
True
>>> name = "&*%"
>>> name.isalnum()
False
6.是否是字母
isalpha
7.是否是数字
isdigit
8.是否小写
islower
9.是否是空格
isspace
10.字符串转换为标题(即首字母大写)
s.title()
11.是否是标题
istitle
12.是否全部都是大写
isupper
13.join连接
列表连接转换为字符串
>>> li
['s1', 's2', 's3', 's4', 's5']
>>> '__'.join(li)
's1__s2__s3__s4__s5'
14.center,ljust,rjust =====》分别为居中,左对齐,右对齐
 
>>> name
'python'
>>> name.center(20,'*')
'*******python*******'
>>> name.ljust(20,'=')
'python=============='
>>> name.rjust(20,'+')
'++++++++++++++python'
 
15.变小写,变大写,大小写互换
>>> name
'RaMos'
>>> name.lower()
'ramos'
>>> name.upper()
'RAMOS'
>>> name.swapcase()
'rAmOS'
 
16.lstrip,strip,rstrip =====>分别为去除左侧,左右两边,右侧空格
 
17.partition ======>分割,变成前,中,后三部分
>>> s1="helloworld"
>>> s1.partition('ll')
('he', 'll', 'oworld')
 
18.replace ======>替换
>>> s1.replace('ll','TT')
'heTToworld'
 
19.split =====>字符串指定分割符变为列表
>>> name = 'hello world'
>>> name
'hello world'
>>> name.split(' ')
['hello', 'world']
20.format ========>格式化字符串
>>> s = "I'm {},{}"
>>> s.format('jinbao',' hello,everybody')
"I'm jinbao, hello,everybody"
 
 
 
 
列表与元组类型:
x = ['add','aardvark','abalone','acme','aerate']
x.sort(key=len) ======>根据字符串长度来排序
>>> x
['add', 'acme', 'aerate', 'abalone', 'aardvark']
 
 
字典类型:
 
dic = {'k1':1234}
添加内容
dic['k2'] = 5678
 
>>> dic = {'k1':1234}
>>> dic
{'k1': 1234}
>>> dic['k2'] = 5678
>>> dic
{'k2': 5678, 'k1': 1234}
 
字典的key值不可以是列表,可以是元组,也可以是系统的当前时间(datetime.datetime.now()),但是千万别这样用,特别不推荐,推荐使用数字,字符串,类的实例。
 
 
判断是否是字典(列表,元组,字符串等等是一样的):
>>> type(dic) is dict
True
 
 
方法:
1.get =====>获取value值,比较dic['k1']的区别是用get方式获取一个不存在的key键的时候不会报异常,返回的是None
 
>>> dic
{'k1': 1234}
>>> dic.get('k1')
1234
>>> dic['k1']
1234
>>> dic.get('k2')
>>> dic['k2']
Traceback (most recent call last):
File "<pyshell#59>", line 1, in <module>
dic['k2']
KeyError: 'k2'
 
>>> dic.get('k2','ok') =====》如果k2在字典里不存在,我让它返回ok,默认是None
'ok'
 
2.clear ===>清空字典
>>> dic
{'k2': 5678, 'k1': 1234}
>>> dic.clear()
>>> dic
{}
 
3.fromkeys ==========>给定一个列表和value,然后依次用列表里面的值当做key,value当做值
>>> dic.fromkeys(range(1,3),"rooney")
{1: 'rooney', 2: 'rooney', 3: 'rooney'}
 
 
 
>>> c = {}
>>> c
{}
>>> for i in range(5):c[i] = []
 
>>> c
{0: [], 1: [], 2: [], 3: [], 4: []}
>>>
 
 
 
4.items(self) ==========>把一个字典变成了列表,并且键和值分别在列表里面又组成了一个元组,这个主要是用于循环取的key和值的时候用。
>>> dic
{'name': 'rooney', 'age': 19}
>>>
>>> dic.items()
dict_items([('name', 'rooney'), ('age', 19)])
 
 
>>> for k,v in dic.items():print (k,v) ======》小数据量循环的时候可以用这种(10000条以内),大数据量循环的时候千万别用这种,因为它会先把字典转换成列表,数据量小的时候没什么影响,数据量一大,就会很慢
name rooney
age 19
 
 
 
>>> for k in dic:print (k,dic[k]) ========>数据量大的时候应该用这种,推荐无论数据量大小,都用这种
 
name rooney
age 19
 
 
5.keys() ======》取得字典中所有的键值
>>> dic.keys()
dict_keys(['name', 'age'])
 
6.pop() ======>出栈,删除age,并且把age的值返回回来
>>> dic
{'name': 'rooney', 'age': 19}
>>> dic.pop("age")
19
>>> dic
{'name': 'rooney'}
 
 
7.popitem() ======>从开始往后删,尽量不要用
 
>>> dic
{'tel': 13874445888, 'name': 'rooney', 'age': 18}
>>> dic.popitem()
('tel', 13874445888)
>>> dic.popitem()
('name', 'rooney')
>>> dic
{'age': 18}
 
8.setdefault =======》如果这个值存在则返回,如果不存在则创建
>>> dic
{'age': 18}
>>> dic.setdefault("name","rooney")
'rooney'
>>> dic
{'name': 'rooney', 'age': 18}
>>> dic.setdefault("name","ramos")
'rooney'
>>> dic
{'name': 'rooney', 'age': 18}
 
 
9.update =======》合并两个字典,将dic2合并进dic中,如果两者都存在同样的键,那么dic2的值将覆盖dic的值
>>> dic
{'name': 'rooney', 'age': 18}
>>> dic2
{'qq': 1305858, 'country': 'china', 'tel': 13874188888}
>>> dic.update(dic2)
>>> dic
{'country': 'china', 'qq': 1305858, 'tel': 13874188888, 'name': 'rooney', 'age': 18}
 
10.values ======》查看所有的值,跟keys相对
 
11.copy ======》转到浅拷贝与深拷贝里里去详细解释
 
 
 
集合类型:集合的其中一个主要功能是去重,
>>> a = list(range(5,10))
>>> a
[5, 6, 7, 8, 9]
 
>>> b = list(range(7,12))
>>> b
[7, 8, 9, 10, 11]
 
set是设置集合
 
c=set(a)
>>> c = set(a)
>>> c
{8, 9, 5, 6, 7}
 
 
>>> d=set(b)
>>> d
{8, 9, 10, 11, 7}
 
取交集:
>>> c & d 《========》c.intersection(d)
{8, 9, 7}
 
取并集:
>>> c | d
{5, 6, 7, 8, 9, 10, 11}
 
反交集:
>>> c ^ d
{5, 6, 10, 11}
 
c里面有,d里面没有的:
>>> c-d
{5, 6}
d里面有,c里面没有的:
>>> d-c
{10, 11}
 

Guess you like

Origin www.cnblogs.com/steven9898/p/11329302.html