Python常用方法(一)
2015-10-28 08:18
609 查看
1、Json处理 json.dumps() # 传入python对象,返回json对象 json.loads() # 传入json对象,返回python对象 2、拼接字符串 print "Hello ""World" # 输出:Hello World print "Hello " + "World" # 输出:Hello World 3、打印字符串 print repr("Hello World") # 输出:'Hello World' temp = 42 print "Hello " + `temp` # 输出:Hello 42 4、列表分片 numbers = [1, 2, 3, 4, 5, 6, 7, 9, 10] print numbers[1:3] # 输出:[2, 3] print numbers[-3:] # 输出:[8, 9, 10] print numbers[7:10] # 输出:[8, 9, 10],虽然10已经超出了列表的索引,但是是在最后一个索引之后 print numbers[:3] # 输出:[1, 2, 3] print numbers[:] # 输出:[1, 2, 3, 4, 5, 6, 7, 9, 10] print numbers[::1] # 输出:[1, 2, 3, 4, 5, 6, 7, 9, 10],步长为1 print numbers[::2] # 输出:[1, 3, 5, 7, 9],步长为2 print numbers[::-1] # 输出:[10, 9, 8, 7, 6, 5, 4, 3, 2, 1],步长为负数则逆向输出 5、列表乘法 print 'python' * 5 # 输出:pythonpythonpythonpythonpython print [42] * 10 # 输出:[42, 42, 42, 42, 42, 42, 42, 42, 42, 42] sequence = [None] * 10 # 创建一个包含是个元素的空列表 print sequence # 输出:[None, None, None, None, None, None, None, None, None, None] 6、返回列表最大值 print max(numbers) # 输出:10 7、返回列表最小值 print min(numbers) # 输出:1 8、输出列表长度 print len(numbers) # 输出:10 9、list函数 lst = list('Hello') print lst # 输出:['H', 'e', 'l', 'l', 'o'] print ''.join(lst) # 输出:Hello 10、删除列表元素 lst = ['Jack', 'Tom', 'Bill'] del lst[1] print lst # 输出:['Jack', 'Tom'] 11、分片赋值 name = list('Perl') name[2:] = list('ar') print name # 输出:['P', 'e', 'a', 'r'] 12、分片赋值来插入新元素 numbers = [1, 5] numbers[1:1] = [2, 3, 4] print numbers # 输出:[1, 2, 3, 4, 5] numbers[1:4] = [] print numbers # 输出:[1, 5],通过分片来删除元素 13、列表末尾追加新数据 lst = [1, 2, 3] lst.append(4) print lst # 输出:[1, 2, 3, 4] 14、统计列表中元素的个数 print ['to', 'be', 'or', 'not', 'to', 'be'].count('to') # 输出:2 15、向列表末尾追加另一个列表 a = [1, 2, 3] b = [4, 5, 6] a.extend(b) print a # 输出:[1, 2, 3, 4, 5, 6] 16、从列表中找出某个值第一个匹配项的索引位置 knights = ['We', 'are', 'the', 'knights', 'who', 'say', 'ni'] print knights.index('who') # 输出:4 17、列表的insert方法 numbers = [1, 2, 3, 4, 5, 6, 7] numbers.insert(3, 'four') print numbers # 输出:[1, 2, 3, 'four', 4, 5, 6, 7] 18、列表pop方法 x = [1, 2, 3] print x.pop() # 输出:3 print x # 输出:[1, 2] 19、列表remove方法 x = ['to', 'be', 'or', 'not', 'be'] x.remove('be') print x # 输出:['to', 'or', 'not', 'be'] 20、列表reverse方法 x = [1, 2, 3] x.reverse() print x # 输出:[3, 2, 1] 21、列表sort方法 x = [4, 6, 2, 1, 7, 9] x.sort() print x # 输出:[1, 2, 4, 6, 7, 9] 22、列表的复制 x = [4, 6, 2, 1, 7, 9] y = x[:] y.sort() print x # 输出:[4, 6, 2, 1, 7, 9] print y # 输出:[1, 2, 4, 6, 7, 9] x = [4, 6, 2, 1, 7, 9] y = x x.sort() print x # 输出:[1, 2, 4, 6, 7, 9] print y # 输出:[1, 2, 4, 6, 7, 9],x和y指向同一个列表,并没有复制 23、创建只有一个元素的元组 x = 42, print x # 输出:(42,) 24、tuple函数 print tuple([1, 2, 3]) # 输出:(1, 2, 3) print tuple('hello') # 输出:('h', 'e', 'l', 'l', 'o') 25、字符串格式化精度 print '%.*s' % (5, 'Guido van Rossum') # 输出:Guido 26、print对齐方式 print '%10.2f' % pi # 输出: 3.14 print '%-10.2f' % pi # 输出:3.14 print '% 5d' % 10 # 输出: 10,在正数前面加上空格 print '%+5d' % 10 # 输出:+10,输出前面加上符号 27、字符串方法 print 'With a moo-moo here, and a moo-moo there'.find('moo') # 输出:7 print 'Hello World'.find('China') # 输出:-1 subject = "$$$ Get rich now!!! $$$" print subject.find('$$$') # 输出:0 print subject.find('$$$', 1) # 输出:20 print subject.find('!!!', 1) # 输出:16 print subject.find('!!!', 0, 16) # 输出:-1 seq = ['1', '2', '3', '4', '5'] sep = '+' print sep.join(seq) # 输出:'1+2+3+4+5',相当于split的逆方法 print 'This is a test'.replace('is', 'eez') # 输出:Theez eez a test print ' internal whitespace is kept '.strip() # 输出:internal whitespace is kept print '*** SPAM * for * everyone!!! ***'.strip(' *!') # 输出:SPAM * for * everyone from string import maketrans table = maketrans('cs', 'kz') print 'this is an incredible test'.translate(table) # 输出:thiz iz an inkredible tezt print 'this is an incredible test'.translate(table, ' ') # 输出:thizizaninkredibletezt
相关文章推荐
- python 多进程实例
- python 如何设置加载其他目录脚本文件
- python 导入模块中的命令
- python 导入模块中的命令并且将命令更名
- python 如何判断调用系统命令是否执行成功
- python中单引,双引,三引号的区别
- python 如何在输出字段中调用变量
- python 中while 使用else实现双分支循环判断
- python 文件处理模块的使用,如何读取文件中数据
- python 如何查看一个变量值中有多少个字符
- python 文件处理模块的使用,给一个文件写入数据
- python 文件处理模块使用,对一个文件追加写入
- python 造数
- python print如何不换行输出
- python 输出正方形
- python 如何加密自己的脚本
- python交互读入
- python程序中在线文档编辑
- Python中几个input方法
- python自动补全