2022-12-23 00:44:40 +08:00
|
|
|
|
"""
|
2022-11-25 03:59:38 +08:00
|
|
|
|
File: array.py
|
|
|
|
|
Created Time: 2022-11-25
|
|
|
|
|
Author: Krahets (krahets@163.com)
|
2022-12-23 00:44:40 +08:00
|
|
|
|
"""
|
2022-11-25 03:59:38 +08:00
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
|
import random
|
2022-11-25 03:59:38 +08:00
|
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
|
def random_access(nums: list[int]) -> int:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""随机访问元素"""
|
2023-01-17 02:10:52 +08:00
|
|
|
|
# 在区间 [0, len(nums)-1] 中随机抽取一个数字
|
|
|
|
|
random_index = random.randint(0, len(nums) - 1)
|
2022-11-25 03:59:38 +08:00
|
|
|
|
# 获取并返回随机元素
|
|
|
|
|
random_num = nums[random_index]
|
|
|
|
|
return random_num
|
|
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
|
2022-11-27 20:00:03 +08:00
|
|
|
|
# 请注意,Python 的 list 是动态数组,可以直接扩展
|
|
|
|
|
# 为了方便学习,本函数将 list 看作是长度不可变的数组
|
2023-03-23 18:51:56 +08:00
|
|
|
|
def extend(nums: list[int], enlarge: int) -> list[int]:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""扩展数组长度"""
|
2022-11-25 03:59:38 +08:00
|
|
|
|
# 初始化一个扩展长度后的数组
|
|
|
|
|
res = [0] * (len(nums) + enlarge)
|
|
|
|
|
# 将原数组中的所有元素复制到新数组
|
|
|
|
|
for i in range(len(nums)):
|
|
|
|
|
res[i] = nums[i]
|
|
|
|
|
# 返回扩展后的新数组
|
|
|
|
|
return res
|
|
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
|
def insert(nums: list[int], num: int, index: int) -> None:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""在数组的索引 index 处插入元素 num"""
|
2022-11-25 03:59:38 +08:00
|
|
|
|
# 把索引 index 以及之后的所有元素向后移动一位
|
2022-12-20 13:20:20 +08:00
|
|
|
|
for i in range(len(nums) - 1, index, -1):
|
2022-11-25 03:59:38 +08:00
|
|
|
|
nums[i] = nums[i - 1]
|
|
|
|
|
# 将 num 赋给 index 处元素
|
|
|
|
|
nums[index] = num
|
|
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
|
def remove(nums: list[int], index: int) -> None:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""删除索引 index 处元素"""
|
2022-11-25 03:59:38 +08:00
|
|
|
|
# 把索引 index 之后的所有元素向前移动一位
|
|
|
|
|
for i in range(index, len(nums) - 1):
|
|
|
|
|
nums[i] = nums[i + 1]
|
2023-03-12 18:49:52 +08:00
|
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
|
def traverse(nums: list[int]) -> None:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""遍历数组"""
|
2022-11-25 03:59:38 +08:00
|
|
|
|
count = 0
|
|
|
|
|
# 通过索引遍历数组
|
|
|
|
|
for i in range(len(nums)):
|
|
|
|
|
count += 1
|
|
|
|
|
# 直接遍历数组
|
|
|
|
|
for num in nums:
|
|
|
|
|
count += 1
|
|
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
|
2023-03-23 18:51:56 +08:00
|
|
|
|
def find(nums: list[int], target: int) -> int:
|
2023-04-09 05:05:35 +08:00
|
|
|
|
"""在数组中查找指定元素"""
|
2022-11-25 03:59:38 +08:00
|
|
|
|
for i in range(len(nums)):
|
|
|
|
|
if nums[i] == target:
|
|
|
|
|
return i
|
|
|
|
|
return -1
|
|
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
|
2023-04-09 05:30:02 +08:00
|
|
|
|
"""Driver Code"""
|
2022-11-25 03:59:38 +08:00
|
|
|
|
if __name__ == "__main__":
|
2023-04-09 05:05:35 +08:00
|
|
|
|
# 初始化数组
|
2023-03-23 18:51:56 +08:00
|
|
|
|
arr: list[int] = [0] * 5
|
2022-11-25 03:59:38 +08:00
|
|
|
|
print("数组 arr =", arr)
|
2023-03-23 18:51:56 +08:00
|
|
|
|
nums: list[int] = [1, 3, 2, 5, 4]
|
2022-11-25 03:59:38 +08:00
|
|
|
|
print("数组 nums =", nums)
|
2023-04-09 05:05:35 +08:00
|
|
|
|
|
|
|
|
|
# 随机访问
|
2023-03-12 18:49:52 +08:00
|
|
|
|
random_num: int = random_access(nums)
|
2022-11-25 03:59:38 +08:00
|
|
|
|
print("在 nums 中获取随机元素", random_num)
|
2023-04-09 05:05:35 +08:00
|
|
|
|
|
|
|
|
|
# 长度扩展
|
2023-03-23 18:51:56 +08:00
|
|
|
|
nums: list[int] = extend(nums, 3)
|
2022-11-25 03:59:38 +08:00
|
|
|
|
print("将数组长度扩展至 8 ,得到 nums =", nums)
|
2023-04-09 05:05:35 +08:00
|
|
|
|
|
|
|
|
|
# 插入元素
|
2022-11-25 03:59:38 +08:00
|
|
|
|
insert(nums, 6, 3)
|
|
|
|
|
print("在索引 3 处插入数字 6 ,得到 nums =", nums)
|
|
|
|
|
|
2023-04-09 05:05:35 +08:00
|
|
|
|
# 删除元素
|
2022-11-25 03:59:38 +08:00
|
|
|
|
remove(nums, 2)
|
|
|
|
|
print("删除索引 2 处的元素,得到 nums =", nums)
|
2023-04-09 05:05:35 +08:00
|
|
|
|
|
|
|
|
|
# 遍历数组
|
2022-11-25 03:59:38 +08:00
|
|
|
|
traverse(nums)
|
2023-04-09 05:05:35 +08:00
|
|
|
|
|
|
|
|
|
# 查找元素
|
2023-03-12 18:49:52 +08:00
|
|
|
|
index: int = find(nums, 3)
|
2022-11-25 03:59:38 +08:00
|
|
|
|
print("在 nums 中查找元素 3 ,得到索引 =", index)
|