Python内置数据结构1
2017-10-30 00:00
411 查看
摘要: ◾列表与常用操作
◾元组与常用操作
序列在Python中是最基本的数据结构。序列中的每个元素都分配一个数字,就是它的位置,也叫索引,第一个索引时0,第二个索引是1,以此类推。
列表中的元素是object对象。
在python3中,range函数是一个可迭代对象。
例子:
list.index 的start 和 end 参数是可以为负数的,但是 在查找的过程中还是从左往右的顺序
对列表做查询
通过索引访问元素
index 方法根据值返回第一个索引
count 方法返回元素在列表里的个数
index 和 count 方法的时间复杂度都是O(n)
线性复杂度:效率与数据规模 呈线性相关
index 与 insert 方法,往左边的位置添加元素
append 和 insert 的效率
append的时间复杂度O(1), 常数时间,效率和数据的规模无关
insert 的时间复杂度是O(n),效率和数据规模成线性相关
尽量使用append
extend 将任意可迭代对象追加到数据的末尾,原地修改,返回None
append 操作单个元素
extend 操作可迭代对象
时间复杂度:定性描述一个算法的效率,是用来分析的,不是来计算
remove() 方法原地修改,返回None,根据值删除元素
从左到右删除第一个
当值不存在时,抛出 ValueError
pop 不传递index参数,时间复杂度是O(1)
pop 传递index参数, 时间复杂度是O(n)
pop 根据索引删除元素, 并且返回删除的元素
list 和 tuple 的区别:
list 可变
tuple 不可变
append,insert,extend都没有
()既是括号,又是元组
◾元组与常用操作
列表
定义:一种数据结构,一个序列,用于顺序的存储数据序列在Python中是最基本的数据结构。序列中的每个元素都分配一个数字,就是它的位置,也叫索引,第一个索引时0,第二个索引是1,以此类推。
列表中的元素是object对象。
在python3中,range函数是一个可迭代对象。
一、增删改查
1、查询(访问列表)
索引从左至右,从0开始;当索引为负数时,从右至左,从-1开始例子:
In [1]: lst = list(range(1, 10)) In [2]: lst Out[2]: [1, 2, 3, 4, 5, 6, 7, 8, 9] In [3]: lst[0] Out[3]: 1 In [4]: len(lst) Out[4]: 9 In [5]: lst[8] Out[5]: 9 In [6]: lst[-1] Out[6]: 9 In [7]: lst[-2] Out[7]: 8
1)list.index()
index() 函数用于从列表中找出某个值第一个匹配项的索引位置In [8]: help(list.index) Help on method_descriptor: index(...) L.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. ~ ~ (END)
In [9]: lst Out[9]: [1, 2, 3, 4, 5, 6, 7, 8, 9] In [10]: lst = [1, 2, 3, 2, 3, 2, 1] In [11]: lst.index(2) Out[11]: 1 In [12]: lst.index(2, 2) Out[12]: 3 In [13]: lst.index(2, 2, 4) Out[13]: 3
list.index 的start 和 end 参数是可以为负数的,但是 在查找的过程中还是从左往右的顺序
2)list.count()
count() 方法用于统计某个元素在列表中出现的次数In [20]: help(list.count) Help on method_descriptor: count(...) L.count(value) -> integer -- return number of occurrences of value ~ (END)
In [25]: lst Out[25]: [1, 2, 3, 2, 3, 2, 1] In [26]: lst.count(3) Out[26]: 2 In [27]: lst.count(2) Out[27]: 3
对列表做查询
通过索引访问元素
index 方法根据值返回第一个索引
count 方法返回元素在列表里的个数
index 和 count 方法的时间复杂度都是O(n)
线性复杂度:效率与数据规模 呈线性相关
2、修改
In [28]: lst Out[28]: [1, 2, 3, 2, 3, 2, 1] In [29]: lst[2] = 5 In [30]: lst Out[30]: [1, 2, 5, 2, 3, 2, 1] In [31]: lst[10] = 10 --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-31-6a600afa3b95> in <module>() ----> 1 lst[10] = 10 IndexError: list assignment index out of range
3、增加
1)list.append()
In [32]: help(list.append) # 追加 Help on method_descriptor: append(...) L.append(object) -> None -- append object to end ~ (END)
In [33]: lst = list(range(12)) In [34]: lst Out[34]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] In [35]: lst.append(12) In [36]: lst Out[36]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] In [37]:
2)list.insert()
In [37]: help(list.insert) Help on method_descriptor: insert(...) L.insert(index, object) -- insert object before index ~ (END)
In [45]: lst = list(range(12)) In [46]: lst Out[46]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] In [47]: lst.insert(1, 'papapa') In [48]: lst Out[48]: [0, 'papapa', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] In [49]: lst.insert(0, 'micracle') In [50]: lst Out[50]: ['micracle', 0, 'papapa', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] In [51]: lst.insert(-1, '-1') In [52]: lst Out[52]: ['micracle', 0, 'papapa', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '-1', 11] In [53]: lst.insert(-100, '-100') In [54]: lst Out[54]: ['-100', 'micracle', 0, 'papapa', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '-1', 11] In [55]:
index 与 insert 方法,往左边的位置添加元素
append 和 insert 的效率
append的时间复杂度O(1), 常数时间,效率和数据的规模无关
insert 的时间复杂度是O(n),效率和数据规模成线性相关
尽量使用append
3)list.extend()
extend使用一个序列扩展另一个list,参数是序列。序列中的元素将逐项添加到列表的尾部。In [55]: help(list.extend) Help on method_descriptor: extend(...) L.extend(iterable) -> None -- extend list by appending elements from the iterable ~ (END)
In [56]: lst = list(range(4)) In [57]: lst Out[57]: [0, 1, 2, 3] In [58]: lst.extend('a') In [59]: lst Out[59]: [0, 1, 2, 3, 'a'] In [60]: lst.extend([1, 2, 4]) In [61]: lst Out[61]: [0, 1, 2, 3, 'a', 1, 2, 4] In [62]: lst.extend([1], [2]) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-62-fe0391927e26> in <module>() ----> 1 lst.extend([1], [2]) TypeError: extend() takes exactly one argument (2 given) In [63]:
extend 将任意可迭代对象追加到数据的末尾,原地修改,返回None
append 操作单个元素
extend 操作可迭代对象
In [64]: lst.append([1]) In [65]: lst Out[65]: [0, 1, 2, 3, 'a', 1, 2, 4, [1]] In [66]: lst.extend([1]) In [67]: lst Out[67]: [0, 1, 2, 3, 'a', 1, 2, 4, [1], 1] In [68]:
时间复杂度:定性描述一个算法的效率,是用来分析的,不是来计算
3、删除
1)list.remove()
In [72]: help(list.remove) Help on built-in function remove: remove(...) method of builtins.list instance L.remove(value) -> None -- remove first occurrence of value. Raises ValueError if the value is not present. ~ (END)
In [73]: lst = [1, 2, 3, 2, 1] In [74]: lst .remove(2) In [75]: lst Out[75]: [1, 3, 2, 1] In [76]: lst.remove('a') --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-76-054406dca733> in <module>() ----> 1 lst.remove('a') ValueError: list.remove(x): x not in list In [77]:
remove() 方法原地修改,返回None,根据值删除元素
从左到右删除第一个
当值不存在时,抛出 ValueError
2)list.pop()
In [77]: help(list.pop) Help on method_descriptor: pop(...) L.pop([index]) -> item -- remove and return item at index (default last). Raises IndexError if list is empty or index is out of range. ~ (END)
In [81]: lst = ['a', 'b', 'c'] In [82]: lst.pop() Out[82]: 'c' In [83]: lst Out[83]: ['a', 'b'] In [84]: lst.pop() Out[84]: 'b' In [85]: lst.pop() Out[85]: 'a' In [86]: lst.pop() --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-86-a1e347685b13> in <module>() ----> 1 lst.pop() IndexError: pop from empty list In [87]: lst = ['a', 'b', 'c'] In [88]: lst.pop(1) Out[88]: 'b' In [89]: lst Out[89]: ['a', 'c']
pop 不传递index参数,时间复杂度是O(1)
pop 传递index参数, 时间复杂度是O(n)
pop 根据索引删除元素, 并且返回删除的元素
3)list.clear()
In [90]: help(list.clear) # 删除所有元素 Help on method_descriptor: clear(...) L.clear() -> None -- remove all items from L ~ (END)
In [92]: lst = list(range(10)) In [93]: lst Out[93]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [94]: lst.clear() In [95]: lst Out[95]: [] In [96]:
二、其他操作
1、len()
In [96]: lst = list(range(10)) In [97]: lst Out[97]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [98]: len(lst) # 统计容器内的项目数量并返回 Out[98]: 10 In [99]:
2、list.reverse()
reverse() 函数用于反向列表中元素。In [103]: lst = list(range(10)) In [104]: lst Out[104]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [105]: lst.reverse() # 原地修改,返回None In [106]: lst Out[106]: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] In [107]:
3、list.sort()
In [109]: lst = [1, 2, 4, 6, 8, 9, 3, 5] In [110]: lst.sort() # 排序, 原地修改,返回None In [111]: lst Out[111]: [1, 2, 3, 4, 5, 6, 8, 9] In [112]:
4、浅拷贝
赋值操作是引用传递,也叫浅复制,浅拷贝In [114]: help(list.copy) Help on method_descriptor: copy(...) L.copy() -> list -- a shallow copy of L (END)
In [115]: l = list(range(12)) In [116]: l Out[116]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] In [117]: id(l) Out[117]: 140547292818120 In [118]: m = l.copy() # 浅拷贝 影子拷贝 In [119]: m Out[119]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] In [120]: id(m) Out[120]: 140547292186248 In [121]:
5、深拷贝
In [126]: from copy import deepcopy In [127]: help(deepcopy) Help on function deepcopy in module copy: deepcopy(x, memo=None, _nil=[]) Deep copy operation on arbitrary Python objects. See the module's __doc__ string for more info. ~ END
In [141]: l = [1, [2, 3], 4] In [142]: m = l.copy() In [143]: m[1][1] = 100 In [144]: m Out[144]: [1, [2, 100], 4] In [145]: l Out[145]: [1, [2, 100], 4] In [146]: n = deepcopy(l) In [147]: n Out[147]: [1, [2, 100], 4] In [148]: n[1][1] = 1000 In [149]: n Out[149]: [1, [2, 1000], 4] In [150]: l Out[150]: [1, [2, 100], 4] In [151]:
元组
元组也是一种有序列表:tuplelist 和 tuple 的区别:
list 可变
tuple 不可变
In [151]: classmates = ('miracle', 'tt', 'kk') In [152]: classmates Out[152]: ('miracle', 'tt', 'kk') In [153]: classmates[0] Out[153]: 'miracle' In [154]: classmates[3] --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-154-81a27e3ce05f> in <module>() ----> 1 classmates[3] IndexError: tuple index out of range
append,insert,extend都没有
()既是括号,又是元组
In [155]: (1) Out[155]: 1 In [156]: t = ('a', 'b', ['A', 'B']) In [157]: t[2][0] = 'X' In [158]: t Out[158]: ('a', 'b', ['X', 'B']) In [159]: t[2] = 1 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-159-45549d36daea> in <module>() ----> 1 t[2] = 1 TypeError: 'tuple' object does not support item assignment In [160]:
相关文章推荐
- week02_python内置数据结构__元组
- Python内置数据结构——字典dict
- python课程第二周 内置数据结构——列表和元组
- python学习3-内置数据结构3-字符串
- Python的内置数据结构
- 关于python内置数据结构和第三方库数据结构的切片索引易混内容
- Python7种内置数据结构总结
- python学习3-内置数据结构2-元组
- python内置数据结构-字符串
- Python内置数据结构3
- python内置的数据结构
- python内置的数据结构
- python 内置数据结构的基本操作 —— dict(2)
- python内置数据结构list
- python内置数据结构
- 【Python】06、python内置数据结构之列表和元祖
- 【Python】07、python内置数据结构之字符串
- 简明Python教程读书笔记-7 几种Python内置的数据结构
- Python内置数据结构——字符串string
- Python笔记2:内置数据结构