python学习记录(四)
0828--https://www.cnblogs.com/fnng/archive/2013/04/18/3029807.html
0828--https://www.cnblogs.com/fnng/archive/2013/04/20/3032563.html
列表
List函数可以将一个字符串拆分成列表。
>>> list('chongshi')
['c', 'h', 'o', 'n', 'g', 's', 'h', 'i']
改变列表:元素赋值
例如,我们想对一个列表中的某个元素赋值。
>>> x =[1,2,3,4]
>>> x[2]=18
>>> x
[1, 2, 18, 4]
删除元素
从列表中删除元素也很容易,使用dele语句来实现。
>>> names = ['zhangsan','lisi','wangwu','sunliu']
>>> del names[2]
>>> names
['zhangsan', 'lisi', 'sunliu']
分片赋值:
>>> name = list('huzi')
>>> name
['h', 'u', 'z', 'i']
>>> name[2:]=list('dazhi')
>>> name
['h', 'u', 'd', 'a', 'z', 'h', 'i']
name的内容为“huzi” ,从第3个字符(2)开始替换,用“da”替换“zi”并被扩容“zhi”,所以新的name的内容为“hudazhi”。
列表方法
方法是一个与某些对象有紧密联系的函数,对象可能是列表、数字,也可能是字符串或者其他类型的对象,一般来说,方法可以这样进行调用:
对象.方法(参数)
方法调用与函数调用很类似。
1、append
append方法用于在列表末尾追加新的对象:
>>> abc = [1,2,3]
>>> abc.append(4)
>>> abc
[1, 2, 3, 4]
2、count
count方法统计某个元素在列表中出现的次数:
>>> ['to','be','or','not','to','be',].count('to')
2
>>> x = [[1,2],1,1,[2,1,[1,2]]]
>>> x.count(1)
2
>>> x.count([1,2])
1
3、extend
extend方法可以在列表的末尾一次性追加另一个序列中的多个值。用新列表扩展原有列表:
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6]
4、index
index 方法用于从列表中找出某个值第一个匹配项的。
>>> knights = ['We','are','the','kninghts','who','say','ni']
>>> knights.index('who')
4
>>> knights[4]
'who'
5、insert
insert 方法用于将对象插入到列表中:
>>> numbers = [1,2,3,5,6,7]
>>> numbers.insert(3,'four')
>>> numbers
[1, 2, 3, 'four', 5, 6, 7]
6、pop
pop 方法会移除列表中一个元素(默认是最后一个),并且返回该元素的值:
>>> x = [1,2,3]
>>> x.pop()
3
>>> x
[1, 2]
>>> x.pop(0)
1
>>> x
[2]
7、remove
remove 方法用于移除列表中某个值的第一个匹配项:
>>> x = ['to','be','or','not','to','be']
>>> x.remove('be')
>>> x
['to', 'or', 'not', 'to', 'be']
8、reverse
revers方法将列表中的元素反向存放
>>> x = [1,2,3]
>>> x.reverse()
>>> x
[3, 2, 1]
9、sort
sort方法用于在原位置对列表进行排序。在“原位置排序”改变原来的列表,从而让其中的元素能按一定的顺序排列。
>>> x = [4,6,2,1,7,9]
>>> x.sort()
>>> x
[1, 2, 4, 6, 7, 9]
元组:不可变序列
元组与列表一样,也是一种序列。唯一的不同是元组不能改变。创建元组的语法很简单:如果你用逗号分割了一些值,那么你就自动创建了元组。
>>> 1,2,3
(1, 2, 3)
>>> (1,2,3) # 用括号表示元组
(1, 2, 3)
>>> () # 空元组
()
如何实现包含一个值的元组呢?方法有点奇特----必须加逗号,即使只有一个值:
>>> 42,
(42,)
>>> (42,)
(42,)
>>> 3 * (20+1)
63
>>> 3 * (20+1,)
(21, 21, 21)
tuple 函数
tuple函数的功能与list函数基本上一样:以一个序列作为参数并把它转换为元组。
>>> tuple([1,2,3])
(1, 2, 3)
>>> tuple('abc')
('a', 'b', 'c')
>>> tuple((1,2,3))
(1, 2, 3)
字符串基本操作
序列的操作索引、分片、乘法、判断成员资格、求长度、取最小值和最大值对字符串同样适用。
注意:字符串都是不可变的。
实用的字符串方法
find
find()方法可以在一个较长的字符串中查找子字符串。它返回子串所在位置的最左端索引。如果没有找到则返回-1。
>>> 'this is a test demo'.find('is')
2
>>> title = "it's a test demo"
>>> title.find('it')
0
>>> title.find('iis')
-1
join(重要)
join用于在队列中添加元素,它是split方法的逆方法。
>>> seq = ['1','2','3','4','5']
>>> sep = '+'
>>> sep.join(seq)
'1+2+3+4+5'
>>> dirs = '','usr','bin','env'
>>> '/'.join(dirs)
'/usr/bin/env'
>>> print 'C:' + '\\'.join(dirs)
C:\usr\bin\env
lower
lower方法返回字符串的小写字母版。
若想要编写“不区分大小写”的代码,使用该方法,代码就会忽略大小写状态
>>> 'Hello,world!'.lower()
'hello,world'
>>> a = 'To Be Or Not To Be'
>>> a.lower()
'to be or not to be'
replace
repleace方法返回某字符串的所有匹配项均被替换之后得到字符串
>>> 'This is a test'.replace('is','eez')
'Theez eez a test'
split(重要)
用于将字符串分割成序列,它是join的逆方法。
>>> '1+2+3+4+5'.split('+')
['1', '2', '3', '4', '5']
>>> '/usr/bin/env'.split('/')
['', 'usr', 'bin', 'env']
>>> 'using the default'.split()
['using', 'the', 'default']
strip
strip方法返回去除两侧(不包含内部)空格的字符串
>>> ' this is a demo '.strip()
'this is a demo'
最新文章
- HTTP 基础知识
- 准备再次开始更新文章,做了10年.net,有项目需要转java
- 完美解决 .txt文件在Mac上不能打开的问题
- JQ 动态加载多选框--随记
- Maven本地资源库
- 揭开NodeJS的神秘面纱!
- 关于内存的5个函数(malloc,VirtualAlloc,GlobalAlloc,LocalAlloc,HeapAlloc)
- cocos2d-x的helloLua例子函数名定义误导初学者
- C++ 不使用virtual实现多态
- [Redis] C#中使用redis
- ibatisnet框架使用说明
- SSM框架注解整合
- ML笔记:Where does the error come from?
- ACM 排列2
- 在weblogic上部署遇到的问题总结
- 六大设计原则(二)LSP里氏替换原则
- vue 图片预览插件
- Windows10 正式企业版激活
- CCF CSP 201312-1 出现次数最多的数
- python base64.b64decode 等号可以随便加