当前位置: 首页 > 图灵资讯 > 技术篇> python_基础知识_2_内置容器

python_基础知识_2_内置容器

来源:图灵教育
时间:2023-04-21 10:07:16

  1 列表List

  a)格式: [元素1,元素2...]

  b) 动态语言, 其中的组成元素可以是多种类型

  c) 常用的方法如下声明集合写法:>>> a = [1,2,3,4,5]>>> type(a)>>> b =[1,2,3,[4,5],6]>>> type(b)>>> for i in b:print i123[4, '5']6>>> 集合方法 x.append(y)将此元素作为变量添加到集合x中, x.extend(y)将y中的每一个元素添加到x集合的末尾,看下面的写作方法>>> a = [x for x in range(5)]>>> print a[0, 1, 2, 3, 4]>>> b = [5,6]>>> print b[5, 6]>>> a.append(b)>>> print a[0, 1, 2, 3, 4, [5, 6]]>>> a.extend(b)>>> print a[0, 1, 2, 3, 4, [5, 6], 5, 查看a的属性和方法,''append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort' 表达自己的方法>>> dir(a)['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort查询集合元素为5的个数>>> a.count(5)在第0位插入元素>>> a.insert(0,'a')>>> print a['a', 0, 1, 2, 3, 4, [5, 6], 5, 6]队尾元素弹出>>> a.insert(1,'b')>>> print a['a', 'b', 0, 1, 2, 3, 4, [5, 6], 5, 6]>>> temp = a.pop()>>> print temp6>>> temp = a.pop()>>> print temp5>>> temp = a.pop()>>> print temp[5, 6]>>> print a['a', 'b', 0, 1, 2, 3, 4]remove(元素1) 删除集合中的第一个元素1。如果有多个元素,则需要重复执行此方法。如果删除后继续删除,会报异常>>> a.append(0)>>> print a['a', 'b', 0, 1, 2, 3, 4, 0]>>> a.remove(0)>>> print a['a', 'b', 1, 2, 3, 4, 0]>>> a.remove(0)>>> print a['a', 'b', 1, 2, 3, 4]>>> a.remve(0)Traceback (most recent call last): File "", line 1, in a.remve(0)AttributeError: 'list' object has no attribute 'remve'排序操作>>> a.sort()>>> print a[1, 2, 3, 4, 'a', 'b内部元素获得写法, 集合[index1:index2] 表示获取集合角标index1(含)到角标index2(不含)元素a[:1] 等同于 a[0:1] a[-1]表示从倒数第一的位置开始 >>> b = a[:1]>>> print b[1]>>> b = a[:3]>>> print b[1, 2, 3]>>> b = a[3:6]>>> print b[4, 'a', 'b']>>> b = a[0:3]>>> print b[1, 2, 3]>>> a[-1]'b'>>> a[-3:-1] 表示从倒数第三位开始(含) 截止到倒数第一位(不含)[4, 'a']

  2元组 Tuple

  a) 格式: a=(元素1,元素2...)

  b) 动态语言, 其中的组成元素可以是多种类型

  c) 与list的区别:

  c.1) 他们都是索引集合对象

  c.2) 两者的实现机制不同,设计目的也不同。元组设计只有查询方法,查询效率很高

  d) 案例: >>> a = (1,2,3) 定义元组>>> type(a) >>> dir(a) 查看元组的属性和方法 只有两种方法 count index['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']>>> for i in a:print i123 >>> c = (y for y in a) 快速复制元组>>> for i in c:print i123>>> a[1] 内部元素的写作方法与数组相同>>> a[:2](1, 2)>>> b = [x for x in a] 元组和list相互转换>>> print b[1, 2, 3]>>> b.append(4) 修改内部元素后,list修改内部元素>>> c = (x for x in b) 在换成元组>>> for i in c:print i1234

  3 Dict字典

  a) 格式: a={key1:val1,key2:val2...}

  b) 案例: >>> a = {'b':1,'d':2} 类似于mapp的声明写法>>> print a{'b': 1, 'd': 2}>>> dir(a)['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']>>> if a.has_key('b'): 查看printt是否包含key作为b的键值 a['b']1>>> a.has_key('b')True>>> a.has_key('c')False>>> for o in a.iteritems(): 遍历map中的所有数据,获得每个键值对printt o('b', 1)('d', 2)>>> b = [x for x in a.iterkeys()] 得到所有的Keyss>>> print b['b', 'd']>>> a.values() 所有的values[1, 2]>>> a.viewkeys()dict_keys(['b', 'd'])>>> help(sorted)Help on built-in function sorted in module __builtin__:sorted(...) sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list

  4 集合 Set >>> a = [1,2,3,4,5,6]>>> b = set(a)>>> type(b)>>> bset([1, 2, 3, 4, 5, 6])set中的元素不能重复,这里先介绍一下这些,以后在工作中遇到了细化

  5 map reduce filter 定义map函数:map(function, sequence[, sequence, ...]) -> list函数的第一个参数是一个函数,其余的是一个或多个序列,返回值是一个集合。function可以理解为一对一或多对一函数。map函数的作用是将function函数调用到参数序列中的每个元素(以相同的方式处理每个元素),并返回包含每个function函数返回值的list。map案例:>>> a= [1,2,3,4,5]>>> map(lambda x:x*2,a)[2, 4, 6, 8, 10]reduce函数的定义:reduce(function, sequence[, initial]) -> valuefunction参数是一个具有两个参数的函数。reduce依次从sequence中取一个元素,以上次调用function的结果作为参数再次调用function。 读起来有点难,看下面的案例: >>>reduce(lambda x, y: x + y, [2, 3, 4, 5, 6]) 等效于 ((2+3) + 4) + 5) + 6) 结果为20>>>说明20reduce函数:reduce函数必须是内联函数,否则会报错,结果不会正常输出。reduce函数的作用:定义参数序列中的元素积累filter函数:filter(function or None, sequence) -> list, tuple, or stringfilter函数的作用:过滤指定序列,filter函数将在序列参数sequence中调用function函数中的每个元素,最终返回的结果包括调用结果为true的元素。>>> filter(lambda x:x%2,a) x模2为真(非0)---> 相当于获得奇数[1, 3, 5]>>> filter(lambda x:not x%2,a) 获取偶数[2, 4]