Article Directory
1. Data structure types in Python
#1.1 List列表 线性表
[]定义,增删查改
#1.2.Tuple元组 只读列表 元素不可变 元素内容也不可变
#()定义,查找
t1=(1,2,3,4)
print(t1[0])
#3.Set集合 元素唯一,不可重复的列表
#{}定义,增删查改
#用 in 来判断是否在集合s1里
s1 = {1,2,3,4}
#4.Dict字典 存储 键值对key-value
#{}定义,增删查改
d1={'Lucy':67,'Tom':89}
d1['Lucy']
d1['Lucy']=100
'Lucy' in d1
'xiaoming' in d1
2. Internal analysis of Python data structure types
data structure: Linked list, array, binary tree, hash, graph
2.1 Is the bottom layer of List list chain storage or array storage?
Head insertion
Tail insertion
Test time difference:
Conclusion: List adoptedArray storageStructure
array
Tail insertion: Time complexity O(1);
Head insertion: Time complexity O(n)
Head insertion requires the element to be moved backward and then the insertion operation
linked list
Head insertion
Tail Insertion
2.2 What is the underlying data structure of the Set collection
Code test 1:
Create a 1 million numberCollection (0-999999), And then use the for loop to determine whether 499999 is in the collection, do a search operation, and search for a hundred times.
Code test 2:
Same as above, except that the test object is replaced with a list.
Test result (approximately):
Collection search time: 10us
List search time: 1s
Conclusion: Set collection in Python usesHash structure: Time complexity O(1)
List lookup (array lookup) and set lookup (hash lookup) contrast:
array lookup, ask one by one-------------》O(n)
hash lookup, hash function---- ---------》O(1)
Supplement: Tree structure
2.3 What is the underlying data structure of the Dict dictionary
Dict and Set are the same, bothhash structureIt's just a value added
to sum up
Data structure is very important, so be sure to type more code! ! !