【Python基础教程第2版】——第二讲:列表和元组

引言:

什么是数据结构

数据结果是通过某种方式(例如对元素进行编号)组织在一起的数据元素的集合。Python中最常用的数据结构是序列

Python包含6种内建的序列:列表和元组(最常用:列表可以修改、元组则不能)、字符串、Unicode字符串、buffer对象和xrange对象。

一、序列的通用操作:

1、索引(从左到右以0开始不断递增;从右到做-1开始不断递减)

>>> greeting = 'hello'
>>> greeting[0]
'h'
>>> greeting[-1]
'o'

2、分片(开始元素为冒号前的,结束元素为冒号后的元素的前一个)

>>> greeting[0:3](0,1,2)
'hel'
>>> greeting[-3:-1](-3,-2)
'll'
>>> greeting[-3:](-3,到最后)
'llo'

>>> greeting[:]
'hello'

>>> greeting[1:3:2]   #以2为步长,前面的都是默认以1为步长
'e'

3、序列相加(只有相同类型的才可以相加)

>>> [1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
>>> 'hello'+'world'
'helloworld'
>>> [1,2,3]+'world'

Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
[1,2,3]+'world'
TypeError: can only concatenate list (not "str") to list

4、乘法

>>> 'python'*5
'pythonpythonpythonpythonpython'
>>> [56]*4
[56, 56, 56, 56]

>>> sequence = [None]*10   #空列表[None]
>>> sequence
[None, None, None, None, None, None, None, None, None, None]

5、成员资格(使用in运算符检查一个值是否在序列中)

>>> ll = '8uusdf'
>>> 'u' in ll
True
>>> 'we'in ll
False
>>> 'uus' in ll
True
>>> 'us' in ll
True
>>> 'ud' in ll
False

6、长度、最小值和最大值(len(序列名称)用的很多)

>>> numbers = [89, 56, 100]
>>> len(numbers)
3
>>> max(numbers)
100
>>> min(numbers)
56

二、列表:Python的“苦力”(这里讲列表的有用的专属的方法)

(一)基本操作

1、list方法(list方法将字符串转化成列表形式;join可以将列表通过某个符号连起来,如通过''空字符将字符列表连起来就是一个字符串)

>>> list1 = list('hello')
>>> list1
['h', 'e', 'l', 'l', 'o']
>>> ''.join(list1)
'hello'
>>>

2、元素赋值

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

3、删除元素

>>> del x[1]
>>> x
[1, 3]

3、分片赋值

>>> name = list('i love you')
>>> name
['i', ' ', 'l', 'o', 'v', 'e', ' ', 'y', 'o', 'u']
>>> name[2:6] = list('hate')
>>> name
['i', ' ', 'h', 'a', 't', 'e', ' ', 'y', 'o', 'u']
>>> ''.join(name)
'i hate you'

以下为不替换任何元素情况下插入元素:

>>> numbers = [1,5]
>>> numbers[1:1] = [2,3,4]
>>> numbers
[1, 2, 3, 4, 5]

以下为不替换任何元素情况下删除元素:

>>> numbers
[1, 2, 3, 4, 5]

>>> numbers[1:5] = []
>>> numbers
[1]

(二)列表方法——很重要(注意定义变量或者对象的名称,不要与内建函数或者某些类的方法同名,否则会导致调用失败)

1、append——在列表末尾追加新的对象(会改变原列表)

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

>>> lst
[1, 2, 3, 4]

2、count——统计某元素在列表中出现的次数

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

3、extend——一次性在末尾追加多个值(会改变原列表)

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

若使用+操作,则不会改变原列表的值:

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> a+b             #生成新的列表
[1, 2, 3, 4, 5, 6]
>>> a
[1, 2, 3]

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

>>> kk = ['what', 'is', 'your', 'name', '?']

>>> kk.index('is')
1

>>> kk2 = ['what', 'is', 'll', 'is', 'doudou']
>>> kk2.index('is')        #注:列表中有两个'is',index会从左往右找,找到就直接返回,即找到第一个就停止,如果没有的话,就会返回错误
1

>>> kk2.index('wer')

Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
kk2.index('wer')
ValueError: 'wer' is not in list

5、insert——将对象插入到列表中(参数为<插入位置,对象的值>,插入位置从0开始计算,想要在哪里插入,就直接写上该位置的索引值即可)

>>> numbers = [1,2,3,5,6,7]
>>> numbers.insert(3, 'four')

>>> numbers
[1, 2, 3, 'four', 5, 6, 7]

6、pop——移除列表中的一个元素,默认是最后一个

注:pop方法是唯一一个既能修改列表又返回元素值(除了None)的列表方法

>>> x = [1,2,3]
>>> x.pop()
3
>>> x
[1, 2]
>>> x.pop(0)    #默认是最后一个,这里可以指定参数,为index索引值
1
>>> x
[2]

7、remove——移除列表中某个值的第一个匹配项(根据值,而不是index索引值)【与pop相反,没有返回值的原位置修改的方法】

>>> x = ['to', 'be', 'or', 'not', 'to', 'be']
>>> x.remove('be')
>>> x
['to', 'or', 'not', 'to', 'be']
>>> x.remove('be')
>>> x
['to', 'or', 'not', 'to']
>>> x.remove('234')

Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
x.remove('234')
ValueError: list.remove(x): x not in list

8、reverse——反向存放列表中的元素(改变列表而不返回值)

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

9、sort——排序方法:在原位置对列表进行排序

>>> x = [3,5,2,7,1,10,6]
>>> x.sort()
>>> x
[1, 2, 3, 5, 6, 7, 10]

若需要保留X的值,则可以先把X的副本赋给Y,然后对Y进行排序

>>> x = [3,5,2,7,1,10,6]
>>> y = x[:]      #注,一定是通过这种方式赋值,不能直接y = x
>>> y.sort()
>>> y
[1, 2, 3, 5, 6, 7, 10]
>>> x
[3, 5, 2, 7, 1, 10, 6]

下面是直接y = x的情况:

>>> x = [3,5,2,7,1,10,6]
>>> y = x
>>> y.sort()
>>> y
[1, 2, 3, 5, 6, 7, 10]
>>> x
[1, 2, 3, 5, 6, 7, 10]        #这里的y和x指向的是同一个列表了

也可以通过sorted()函数,代码如下:

>>> x = [3,5,2,7,1,10,6]
>>> y = sorted(x)
>>> y
[1, 2, 3, 5, 6, 7, 10]
>>> x
[3, 5, 2, 7, 1, 10, 6]

10、cmp——比较大小,cmp(x,y)中,x大于y会返回1;x小于y返回0

>>> cmp(42,32)
1

>>> cmp(99,100)
-1
>>> numbers = [5,2,9,7]
>>> numbers.sort(cmp)
>>> numbers
[2, 5, 7, 9]

三、元组

 1、元组的定义:

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

2、tuple()函数  ——与列表的list方法类似,可以通过该函数将列表或者字符串转化成元组形式

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

3、元组的基本操作——与列表不同,没有列表的一堆方法,元组的基本操作(创建元组和访问元组元素)可参见其他类型的序列

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

原文地址:https://www.cnblogs.com/keke-xiaoxiami/p/3925314.html