python快速学习3

数据结构

列表扩展

列表对象的方法

  • list.append(x)
  • list.extend(L) 将L列表中的元素全都加到list中
  • list.insert(i,x) 在list的编号为i的元素之前加入元素x
  • list.remove(x) 删除list中值为x的第一个元素
  • list.pop([i]) 删除list中编号为i的元素并返回该元素的值,[] 是可选的,如果没有指定i,那就弹出最后一个元素
  • list.index(x) 返回list中第一个值为x的元素的索引,没找到返回错误
  • list.count(x) 返回x在list中出现的次数
  • list.sort() 对list元素进行升序排列
  • list.reverse() 将list中的元素倒置
>>> NumList
[0, 1, 2, 3, 4, 5, 6, 7]
>>> NumList.append('song')
>>> NumList
[0, 1, 2, 3, 4, 5, 6, 7, 'song']
>>> NumList.extend(range(8,16))
>>> NumList
[0, 1, 2, 3, 4, 5, 6, 7, 'song', 8, 9, 10, 11, 12, 13, 14, 15]
>>> NumList.insert(9,'shouli')
>>> NumList
[0, 1, 2, 3, 4, 5, 6, 7, 'song', 'shouli', 8, 9, 10, 11, 12, 13, 14, 15]
>>> NumList.remove('song')
>>> NumList.remove('shouli')
>>> NumList
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
>>> NumList.pop(14)
14
>>> NumList.pop()
15
>>> NumList
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
>>> NumList.insert(8,'song')
>>> NumList.insert(8,'shouli')
>>> NumList
[0, 1, 2, 3, 4, 5, 6, 7, 'shouli', 'song', 8, 9, 10, 11, 12, 13]
>>> NumList.index('song')
9
>>> NumList.count(6)
1
>>> NumList.sort()
>>> NumList
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 'shouli', 'song']
>>> NumList.reverse()
>>> NumList
['song', 'shouli', 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

列表可以当做堆栈来用
利用append和pop可以实现一个栈

>>> Stack = range(3)
>>> Stack.append(13)
>>> Stack
[0, 1, 2, 13]
>>> Stack.pop()
13
>>> Stack
[0, 1, 2]
>>>

列表当做队列使用
在list头部虽然可以用insert来添加,但是效率很低,因为系统要移动整个列表,尾部添加可以用append实现.
所以引入了collections.deque
引入collections模块中的deque函数,该函数可以将列表变成队列
再调用append和popleft()方法实现队列的进队出队操作

>>> from collections import deque
>>> queue = deque(range(10))
>>> queue.append(32)
>>> queue
deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 32])
>>> queue.popleft()
0
>>> queue.popleft()
1
>>> queue
deque([2, 3, 4, 5, 6, 7, 8, 9, 32])
>>>

内置的针对列表和函数组合使用的方法(函数式编程工具)
filter()
list的元素依次进入函数,返回值为ture的元素被保留,保留的元素组成一个新的列表返回

>>> fun = lambda x: x%2 == 0
>>> fun(2)
True
>>> filter(fun, range(16))
[0, 2, 4, 6, 8, 10, 12, 14]
>>>

map()
list的元素依次进入函数,将返回值组成一个新的列表

>>> map(fun,range(16))
[True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False]
>>>

reduce()
首先以序列的前两个元素调用函数function,再以返回值和第三个参数调用,依次执行下去。迭代计算
最终返回值为一个单值

>>> def sumfun(a,b):return a+b
...
>>> reduce(sumfun,range(100))
4950
>>> reduce(sumfun,range(100),0)
4950
>>>

列表推导式
按照规则快速生成所需要的列表
记住格式即可

[(x,y) for x in list1 for y in list2 if x>y]
>>>[(x,y) for x in range(100) for y in range(100) if x%15 == 0 and y%32 == 0]
[(0, 0), (0, 32), (0, 64), (0, 96), (15, 0), (15, 32), (15, 64), (15, 96), (30, 0), (30, 32), (30, 64), (30, 96), (45, 0), (45, 32), (45, 64), (45, 96), (60, 0), (60, 32), (60, 64), (60, 96), (75, 0), (75, 32), (75, 64), (75, 96), (90, 0), (90, 32), (90, 64), (90, 96)]
>>>

del语句

del用于删除列表及其子项
remove(x)是删除值为x的第一个元素
pop(i)是删除第i个元素

>>> numList = [(x,y) for x in range(100) for y in range(100) if x%15 == 0 and y%16 == 0]
>>> del numList[5:]
>>> numList
[(0, 0), (0, 16), (0, 32), (0, 48), (0, 64)]
>>> del numList[:]
>>> numList
[]
>>> del numList
>>> numList
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'numList' is not defined
>>>

元组结构

元组相当于值不可变的list

>>> t = (1,2,3,4,5)
>>> t
(1, 2, 3, 4, 5)
>>> t += 6, 7,8
>>> t
(1, 2, 3, 4, 5, 6, 7, 8)
>>> t[0] = 3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

集合

集合是无序不重复元素的集
基本功能:关系测试, 消除重复元素
利用set()来创建集合
基本运算 - } & ^

>>> YanFa = ['song', 'xin', 'leng', 'wu']
>>> GongChen = [ 'ding', 'xu', 'ji','song']
>>> yf = set(YanFa)
>>> gc = set(GongChen)
>>> 'ding' in yf
False
>>> 'song' in yf
True
>>> yf + gc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'set' and 'set' //没有+ 运算
>>> yf - gc
set(['wu', 'leng', 'xin'])
>>> gc - yf
set(['ding', 'ji', 'xu'])
>>> yf | gc
set(['ding', 'leng', 'song', 'xin', 'wu', 'ji', 'xu'])
>>> yf & gc
set(['song'])
>>> yf ^ gc
set(['ding', 'leng', 'xin', 'wu', 'ji', 'xu']) // ^ yf和gc合并并剔除公共元素
>>>

字典

关键字:值
关键字必须是不可变类型,所以列表是不行的
用{}来创建字典
可用的操作:del keys() in sorted

//创建字典
>>> grade = {'song':88, 'leng':99, 'wu':92}
>>> grade2 = dict([('song',88),('leng',99),('wu',92)])
>>> grade3 = dict(song=88,leng=99,wu=92) //利用关键字参数来创建
>>> grade3
{'wu': 92, 'leng': 99, 'song': 88}

>>> grade['song']
88
>>> grade['song'] = 89
>>> grade['song']
89
>>> del grade['song']
>>> grade
{'wu': 92, 'leng': 99}
>>> grade2.keys()
['wu', 'leng', 'song']
>>> sorted(grade2.keys())
['leng', 'song', 'wu']
>>>

循环技巧

enumerate()函数直接得到索引位置和对应的值

>>> for i, name in enumerate(['song','wu','leng']):
...     print i, name
...
0 song
1 wu
2 leng
>>>

深入条件控制

比较操作符
in
not in

is
is not

a < b == c //判断操作符可以传递

and
or

关于比较

(1, 2) < (1, 2, 3)
(1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)

原文地址:https://www.cnblogs.com/sslblog/p/6930403.html