python基础笔记

序列中的每个元素都有自己的编号,有6中内建序列:列表、元组、字符串、Uniclude字符串、buffer对象和xrange对象。列表和元组的主要区别在于,列表可以修改,元组则不能。序列和映射是两类主要的容器,映射中的每个元素都有自己的一个名字(键),集合既不是序列也不是容器类型。

所有序列类型都可以进行某些特定的操作。这些操作包括:索引、切片、加、乘、迭代,检查某个元素是否属于序列的成员。两种相同类型的序列才能进行连接操作。None是一个python内建值,可以使用none来初始化一个长度为10的列表。

>>> sequence = [None]*10
>>> sequence
[None, None, None, None, None, None, None, None, None, None]

列表(列表内容可变的数据结构)

1. list函数,把序列转换为列表,适用于所有类型的序列。

>>> list('Hello')
['H', 'e', 'l', 'l', 'o']

2. 改变列表的方法:元素赋值,元素删除,分片赋值以及列表方法(请注意,并不是所有的列表方法都真正地改变列表)。不能为一个位置不存在的元素进行赋值。

列表的分片赋值可以用于修改某一个元素,某几个元素,添加元素,删除元素等。

>>> names = ['Alice', 'Beth', 'Cecil', 'Dee-Dee', 'Ear']
>>> del names[2]
>>> names
['Alice', 'Beth', 'Dee-Dee', 'Ear']
>>> names[1:2] = ['Angelia', 'PGL', 'PeiQi', 'LXL']
>>> names
['Alice', 'Angelia', 'PGL', 'PeiQi', 'LXL', 'Dee-Dee', 'Ear']
>>> names[0:0] = ['Hito', 'XieNa', 'HJiong']
>>> names
['Hito', 'XieNa', 'HJiong', 'Alice', 'Angelia', 'PGL', 'PeiQi', 'LXL', 'Dee-Dee', 'Ear']
>>> names[0:1] = []
>>> names
['XieNa', 'HJiong', 'Alice', 'Angelia', 'PGL', 'PeiQi', 'LXL', 'Dee-Dee', 'Ear']
>>> 

3. 列表方法,调用方法为 对象名.方法(参数) 。append方法为在列表末末位追加新的对象,直接修改原来的列表(这样做,可能会带来麻烦);count方法统计某个元素再列表中出现的次数;extend方法可以在列表的末位一次性追加另一个序列中的多个值,即可以用新列表扩展原有的列表,extend方法是在原有列表中修改,连接操作会创建一个新的结果副本返回,效率比extend低;

 1 >>> x = [[1,2],1,1,[2,1,[1,2]]]
 2 >>> x.count(1)
 3 2
 4 >>> a = [1,0,1,2,4]
 5 >>> x.extend(a)
 6 >>> x
 7 [[1, 2], 1, 1, [2, 1, [1, 2]], 1, 0, 1, 2, 4]
 8 >>> b = [4,5,6]
 9 >>> a+b
10 [1, 0, 1, 2, 4, 4, 5, 6]
11 >>> 

index方法用于从列表中找出某个值第一个匹配项的索引位置,这个词如果不在列表中,则会出错;insert方法在列表指定位置插入元素,如果指定位置超过列表长度,则默认在最后一个位置插入;pop方法移除列表中的一个元素,默认是最后一个,并返回被移除的元素。python没有push入栈方法,可以使用append方法代替;remove方法用于移除中某个值的第一个匹配项,匹配的粒度是元素级别的,而不是字节级别的,在列表原位置修改,但没有返回值;reverse方法将列表中的元素反向存放,改变了列表但不返回值;

 1 >>> knights = ['we', 'are', 'the', 'knights', 'eho', 'say', 'Hi']
 2 >>> knights.index('who')
 3 
 4 Traceback (most recent call last):
 5   File "<pyshell#39>", line 1, in <module>
 6     knights.index('who')
 7 ValueError: 'who' is not in list
 8 >>> knights.index('are')
 9 1
10 >>> knights.insert(3, 'dark')
11 >>> knights
12 ['we', 'are', 'the', 'dark', 'knights', 'eho', 'say', 'Hi']
13 >>> knights.insert(100, 'eye')
14 >>> knights
15 ['we', 'are', 'the', 'dark', 'knights', 'eho', 'say', 'Hi', 'eye']
16 >>> knights.pop()
17 'eye'
18 >>> knights
19 ['we', 'are', 'the', 'dark', 'knights', 'eho', 'say', 'Hi']
20 >>> knights.pop(1)
21 'are'
22 >>> knights
23 ['we', 'the', 'dark', 'knights', 'eho', 'say', 'Hi']
24 >>> knights.remove('ar')
25 
26 Traceback (most recent call last):
27   File "<pyshell#49>", line 1, in <module>
28     knights.remove('ar')
29 ValueError: list.remove(x): x not in list
30 >>> knights.remove('dar')
31 
32 Traceback (most recent call last):
33   File "<pyshell#50>", line 1, in <module>
34     knights.remove('dar')
35 ValueError: list.remove(x): x not in list
36 >>> knights.remove('dark')
37 >>> knights.reverse()
38 >>> knights
39 ['Hi', 'say', 'eho', 'knights', 'the', 'we']
40 >>> knights.sort()
41 >>> knights
42 ['Hi', 'eho', 'knights', 'say', 'the', 'we']
43 >>> 

sort方法用于在原位置对列表进行排序,改变原来的列表。现在实现一个排序的功能,返回给用户一个排序好的列表副本,同时又保留原有列表。

1 >>> n = [3,7,1,9,3,7,4]
2 >>> y = n.sort() #sort方法没有返回值,所以y=None
3 >>> n
4 [1, 3, 3, 4, 7, 7, 9]
5 >>> y
6 >>> print y
7 None
8 >>> 

先复制x,再排序。赋值方法中,y=n实际上y和n指向同一个列表,所以对y做排序,n的列表值也就已经修改了。

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

正确的操作姿势为将列表中的每个元素都复制给y。

1 >>> n = [3,7,1,9,3,7,4]
2 >>> y = n[:]
3 >>> y.sort()
4 >>> y
5 [1, 3, 3, 4, 7, 7, 9]
6 >>> n
7 [3, 7, 1, 9, 3, 7, 4]
8 >>> 

可以使用sorted函数获取已排序列表的副本。

1 >>> n
2 [3, 7, 1, 9, 3, 7, 4]
3 >>> z = sorted(n)
4 >>> z
5 [1, 3, 3, 4, 7, 7, 9]
6 >>> n
7 [3, 7, 1, 9, 3, 7, 4]
8 >>>

高级排序:为sort方法提供比较参数。sort的参数有cmp、key和reverse。

使用cmp作为参数:

1 >>> numbers = [5, 2, 9, 4]
2 >>> numbers.sort(cmp)
3 >>> numbers
4 [2, 4, 5, 9]

使用key作为参数:提供一个在排序过程中使用的函数,该函数并不是直接用来确定对象的大小,而是为每个元素创建一个键,然后所有元素根据键来排序。例如使用len作为键函数。

1 >>> x = ['aardvark', 'bulabula', 'acm', 'average', 'abalone', 'a']
2 >>> x.sort(key=len)
3 >>> x
4 ['a', 'acm', 'average', 'abalone', 'aardvark', 'bulabula']
5 >>> x = [4,6,4,3]
6 >>> x.sort(reverse = True)
7 >>> x
8 [6, 4, 4, 3]
9 >>> 

元组:不可变序列

字符串也是元组。创建元组的方法是用都好分割一些值。

1 >>> 1,2,3
2 (1, 2, 3)
3 >>> () #空元组
4 ()
5 >>> (42, ) #只有一个元素的元组
6 (42,)
7 >>> 

tuple函数,以一个序列作为参数并把它转换为元组: 1 >>> tuple([1,2,3]) 2结果 (1, 2, 3) 

元组的意义:元组可以在映射(和集合的成员)中当做键使用--而列表则不行。元组作为很多内建函数和方法的返回值存在

原文地址:https://www.cnblogs.com/mymindview/p/8371707.html