Python内置的数据结构有多种类型,本文主要其内建的类型及其相关的方法
- list|列表
列表的crud(Create,Retrieve,Upadte,Delete)方法
- list.append(x) ---把一个元素添加到list的结尾
- list.extend(L) ---将一个给定list的所有元素添加到另一个list中
- list.insert(i, x) ---在指定位置插入一个元素
- list.remove(x) ---删除list中值为x的第一个元素,若无则返回一个错误
- list.pop([i]) ---从制定位置删除元素,并将其返回
- list.index(x) --- 返回list中第一个值为x的索引
- list.count(x) ---返回x在list中出现的次数
- list.sort(x) ---对list进行排序
- list.reverse() --- 倒序
>>> a = [12, 34, 34, 56, 456.8]
>>> print a.count(12), a.count(34),a.count('x')
1 2 0
>>> a.insert(2,-1)
>>> a.append(34)
>>> a
[12, 34, -1, 34, 56, 456.8, 34]
>>> a.index(34)
1
>>> a.remove(456.8)
>>> a
[12, 34, -1, 34, 56, 34]
>>> a.reverse()
>>> a
[34, 56, 34, -1, 34, 12]
>>> a.sort()
>>> a
[-1, 12, 34, 34, 34, 56]
>>>