Python3基础教程-第2章笔记-下

1、列表:python的”苦力”

(1)list函数: 适用于所有类型的序列

>>> list('hello')
['h', 'e', 'l', 'l', 'o']
>>> 

(1)基本的列表操作:元素赋值、元素删除、分片赋值、列表方法

1)改变列表:元素赋值

 利用索引标记为某个特定的、位置明确的元素赋值

>>> x = [1,1,1,]
>>> x[1] = 2
>>> x
[1, 2, 1]
>>> 

2)删除元素:del语句

>>> names = ['Alice','Beth','Cecil','Dee-Dee','Earl']
>>> del names[2]
>>> names
['Alice', 'Beth', 'Dee-Dee', 'Earl']
>>> 

3)分片赋值:

一次为多个元素赋值

>>> name = list('Perl')
>>> name
['P', 'e', 'r', 'l']
>>> name[2:] = list('ar')
>>> name
['P', 'e', 'a', 'r']
>>> 

可以使用与元序列不等长的序列将分片替换

>>> name = list('Perl')
>>> name[1:] = list('ython')
>>> name
['P', 'y', 't', 'h', 'o', 'n']
>>> 

不替换任何原有元素的情况下插入新的元素,

>>> numbers = [1,5]
>>> numbers[1:1] = [2,3,4]
>>> numbers
[1, 2, 3, 4, 5]
#这个程序只是“替换”了一个空的分片,实际的操作是插入了一个序列

通过分片来删除元素也可行,

>>> numbers
[1, 2, 3, 4, 5]
>>> numbers[1:4] = []
>>> numbers
[1, 5]
#这个与del numebers[1:4]是一样的

(2)列表方法

 方法:与某些对象有紧密联系的函数,对象可能是列表、数字,也可能是字符串或者其他类型的对象

调用方式:对象.方法(参数)

1)append:用于在列表末尾追加新的对象,直接修改原表!!而不是返回一个修改过的表

>>> lst = [1,2,3]
>>> lst.append(4)
>>> lst
[1, 2, 3, 4]
>>> 

2)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:追加另一个序列中的多个值,用新列表扩展原列表

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6]
>>> 

*extend与连接+的区别:extend修改列表,连接返回全新的列表

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> a + b
[1, 2, 3, 4, 5, 6]
>>> a
[1, 2, 3]
>>> 

4)index:从列表中找出某个值第一个匹配项的索引位置

>>> knights = ['we','are','the','knights','who','say','ni']
>>> knights.index('who')
4
>>> knights,index('herring')
Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    knights,index('herring')
NameError: name 'index' is not defined
>>> 

5)insert:将对象插入到列表中

>>> numbers = [1,2,3,4,5,6,7]
>>> numbers.insert(3,'four')
>>> 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]
>>> 

栈:数据结构,出栈入栈,‘后进先出’

*append代替进栈

>>> x = [1,2,3]
>>> x.append(x.pop())
>>> x
[1, 2, 3]
>>> 

7)remove:移除列表中某个值的第一个匹配项

>>> x= ['to','be','or','not','to','be']
>>> x.remove('be')
>>> x
['to', 'or', 'not', 'to', 'be']
>>> x.remove('bee')
Traceback (most recent call last):
  File "<pyshell#44>", line 1, in <module>
    x.remove('bee')
ValueError: list.remove(x): x not in list
>>> 

*remove只改变列表,没有返回值,与pop相反

8)reverse:将列表中的元素反向存放

>>> x = [1,2,3]
>>> x.reverse()
>>> x
[3, 2, 1]
>>> 

*反向迭代?

9)sort:在原位置对列表进行排序,”原位置排序”即改变原来的列表,按一定顺序排列

>>> x = [4,6,2,1,7,9]
>>> x.sort()
>>> x
[1, 2, 4, 6, 7, 9]
>>> 

错误示范:

>>> x = [4,6,2,1,7,9]
>>> y = x.sort()#don't do this
>>> print (y)
None

正确示范:

>>> x = [4,6,2,1,7,9]
>>> y = x[:]#调用x[:]是得到了包含x所有元素的分片(副本)
>>> y.sort()
>>> x
[4, 6, 2, 1, 7, 9]
>>> y
[1, 2, 4, 6, 7, 9]
>>> 

不能直接将x赋值给y,因为这样x和y指向同一个列表

>>> y = x
>>> y.sort()
>>> x
[1, 2, 4, 6, 7, 9]
>>> y
[1, 2, 4, 6, 7, 9]
>>> 

*sorted函数:获取已排序列表副本

>>> x = [4,6,2,1,7,9]
>>> y = sorted(x)
>>> x
[4, 6, 2, 1, 7, 9]
>>> y
[1, 2, 4, 6, 7, 9]

sorted函数可用于任何序列,却总是只返回一个列表

>>> sorted('python')
['h', 'n', 'o', 'p', 't', 'y']

10)高级排序:

compare(x,y):比较函数)(py.3没有该函数模块)

  x<y,返回负数

  x>y,返回正数

  x=y,返回0

*python 3.4.3 的版本中已经没有cmp函数,被operator模块代替,在交互模式下使用时,需要导入模块。

在没有导入模块情况下,会出现下面的情况:

>>> cmp(42,32)
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    cmp(42,32)
NameError: name 'cmp' is not defined

*sort方法另外两个可选参数-key和reverse

关键字参数:

(1)key参数

#根据元素的长度进行排序,以len作为键函数
>>> x = ['aardvark','abalone','acme','add','aerate'] >>> x.sort(key=len) >>> x ['add', 'acme', 'aerate', 'abalone', 'aardvark']

(2)reverse参数:简单的布尔值(true or false),用以指明是否要进行反向排序

>>> x = [4,6,2,1,7,9]
>>> x.sort(reverse=True)
>>> x
[9, 7, 6, 4, 2, 1]
>>> 

2、元组:不可变序列

元组:序列,与列表一样,但不可修改

语法创建:逗号隔开值,即自动创建元组

>>> 1,2,3
(1, 2, 3)
>>> 

元组大部分时候是通过括号括起来的

>>> (1,2,3)
(1, 2, 3)
>>> 

一个值的元组——必须加个逗号,即使只有一个值

>>> 42
42

 >>> (42)
  42

>>> 42,
(42,)
>>> (42,)
(42,)
>>> 
#1)后两个为长度为1的元组,前两个非元组;2)逗号很重要,只添加括号没有逗号也不会成为元组的

*逗号能够彻底改变表达式的值,如下,

>>> 3*(40+2)
126
>>> 3*(40+2,)
(42, 42, 42)
>>> 

1)tuple函数

tuple:功能基本同list,以一个序列作为函数并把它转换为元组,如果参数就是元组,那么该参数就会被原样返回

>>> tuple([1,2,3])
(1, 2, 3)
>>> tuple('abc')
('a', 'b', 'c')
>>> tuple((1,2,3))
(1, 2, 3)

2)基本元组操作

*元祖的分片还是分片,就像列表的分片还是列表一样

>>> x = 1,2,3
>>> x[1]
2
>>> x[0:2]
(1, 2)
>>> 

猜你喜欢

转载自www.cnblogs.com/Hermione74/p/9651477.html