In contiguous memory space , Store a set of elements of the same type .
A two-dimensional array is actually a linear array that stores the first addresses of other arrays .
[1,2,3]
0,1,2
Array access a[1]=2
Array search Find this element
There are four common operation time complexity of arrays
visit O(1) The address location can be obtained by calculation , So as to visit
Search for O(N) You need to traverse the array
Insert O(N) You need to move the following elements back
If there's not enough memory , Need to open up a new space , Move the array in
Delete O(N) You need to move the following elements forward
Suitable for reading
It is not suitable for frequent addition and deletion .
scene : Read more and write less
a=[] # Note that the brackets must be of the same type Otherwise, list
a.append() # The time complexity is O(1) perhaps O(n),O(1) Add 、O(n) It refers to finding another location to store
a.insert(2,99); # O(n)
Use index ( Subscript ) Access elements
# O(1)
temp=a[2]
Use index ( Subscript ) Access elements
# O(1)
a[2]=88
a.remove(88) # O(n) find 88 This element
a.pop(1) # O(n) Delete the index as 1 You need to move the whole forward by one unit
a.pop() # O(1) Delete the end No need to move
a=[1,2,3]
size=len(a)
print(size)
# O(n)
for i in a:
print(i)
for index,element in enumerate(a):
print("index at",index,"is:",element)
for i in range(0,len(a)):
print("i:",i,"element",a[i])
index=a.index(2) # O(n) Traverse from beginning to end
print(index)
# O(nLog n)
a=[3,1,2]
a.sort() # From small to large
print(a)
a.sort(reverse=True) # From big to small
print(a)