### What - An array is a **contiguous block of memory**, a collection of elements stored in a **specific order** and **accessed by their index**. - 一块连续的内存空间,有了这块内存空间的首地址,就能直接通过索引计算出任意位置的元素地址 / 访问这块内存空间中的任意元素。 ### Complexity Analyze - Insert & Delete in the middle: On - Others: O1 - Insert (Add) - append at the end: O1 - insert in the middle: On - Delete - remove from the end: O1 - remove from the middle: On - Access (Read) - give a index, retrieve the value of the index: O1 - give a index, modify the value of the index: O1 ## Grammar ### CRUD #### Read - retrieve ```python arr = [1, 2, 3, 4, 5] element = arr[2] # 访问索引为2的元素 ``` - modify ```python arr = [1, 2, 3, 4, 5] arr[1] = 10 # 将索引为1的元素修改为10 ``` #### add - append in the end ```python arr = [1, 2, 3] arr.append(4) # 在数组末尾添加元素4 ``` - insert 插入元素(在特定位置) ```python arr = [1, 2, 4] arr.insert(2, 3) # 在索引2的位置插入元素3 ``` #### delete - `pop () ` delete the last one / based on index and return the value ```python arr.pop() arr.pop(1) ``` - `remove(2)` (based on value) ```python arr = [1, 2, 3, 4, 3] arr.remove(3) # 删除第一个出现的3 ``` - `del arr[2]` (base on index) ```python arr = [1, 2, 3, 4, 5] del arr[2] # 删除索引为2的元素 ``` #### other 7. 获取数组长度 ```python arr = [1, 2, 3, 4, 5] length = len(arr) # 获取数组长度 ``` 8. 查找元素索引 ```python arr = [1, 2, 3, 4, 5] index = arr.index(3) # 查找元素3的索引 ``` 9. 计数元素出现次数 ```python arr = [1, 2, 2, 3, 2, 4] count = arr.count(2) # 计算元素2出现的次数 ``` ### 切片 Python 的切片语法是 `array[start:stop:step]`,其中每个参数都是可选的。 1. 基本切片 ```python arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(arr[2:7]) # 输出: [2, 3, 4, 5, 6] ``` 这会返回从索引 2 到索引 6 的元素(不包括索引 7)。 2. 省略 start 或 stop ```python print(arr[:4]) # 输出: [0, 1, 2, 3] print(arr[6:]) # 输出: [6, 7, 8, 9] ``` 省略 start 意味着从开始切片,省略 stop 意味着切片到结束。 3. 负索引 ```python print(arr[-5:]) # 输出: [5, 6, 7, 8, 9] print(arr[:-2]) # 输出: [0, 1, 2, 3, 4, 5, 6, 7] ``` 负索引从数组末尾开始计数。 4. 步长(step) ```python print(arr[::2]) # 输出: [0, 2, 4, 6, 8] print(arr[1::2]) # 输出: [1, 3, 5, 7, 9] ``` 步长决定了切片时的跳跃距离。 5. 负步长(反向遍历) ```python print(arr[::-1]) # 输出: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] print(arr[7:2:-1]) # 输出: [7, 6, 5, 4, 3] ``` 负步长会反向遍历数组。 6. 切片赋值 ```python arr[2:5] = [20, 30, 40] print(arr) # 输出: [0, 1, 20, 30, 40, 5, 6, 7, 8, 9] ``` 可以用切片来替换数组的一部分。 7. 删除切片 ```python del arr[3:6] print(arr) # 输出: [0, 1, 20, 7, 8, 9] ``` 使用 del 关键字可以删除一个切片。 8. 扩展切片 ```python arr[2:2] = [100, 200] print(arr) # 输出: [0, 1, 100, 200, 20, 7, 8, 9] ``` 在不替换现有元素的情况下插入新元素。 9. 使用切片复制数组 ```python new_arr = arr[:] ``` 这会创建一个数组的浅拷贝。 10. 多维数组切片 ```python matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print(matrix[0:2]) # 输出: [[1, 2, 3], [4, 5, 6]] print([row[0:2] for row in matrix[0:2]]) # 输出: [[1, 2], [4, 5]] ``` 可以对多维数组的每个维度进行切片。 ### Iteration 1. 使用 for 循环遍历元素 ```python arr = [1, 2, 3, 4, 5] for element in arr: print(element) ``` 这种方法直接遍历数组中的每个元素。 2. 使用索引遍历 ```python arr = [1, 2, 3, 4, 5] for i in range(len(arr)): print(arr[i]) ``` 这种方法使用索引来访问每个元素,适合当你需要知道元素位置的情况。 3. 使用 enumerate 同时获取索引和元素 ```python arr = [1, 2, 3, 4, 5] for index, element in enumerate(arr): print(f"Index: {index}, Element: {element}") ``` 这种方法同时提供了元素的索引和值,经常和 [[Hash Table]] 配合 4. 反向遍历 ```python arr = [1, 2, 3, 4, 5] for element in reversed(arr): print(element) ``` 这种方法从数组的末尾开始向前遍历。 5. 步长遍历 ```python arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for element in arr[::2]: # 步长为2 print(element) ``` 这种方法可以按指定的步长遍历数组。 6. while 循环遍历 ```python arr = [1, 2, 3, 4, 5] i = 0 while i < len(arr): print(arr[i]) i += 1 ``` 这种方法使用 while 循环来遍历,适合需要更复杂控制流的情况。