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'

 

 

 

 

 

posted @ 2018-08-28 14:40  liu_lu  阅读(163)  评论(0编辑  收藏  举报