【ADS-1】【python基础-2】基本语法与数据结构(列表、字典、集合)
目录
- 1 实践
- 2 知识点
- 2.1 列表
- 2.2 字典
- 2.3 元组
前言
第一阶段:Python基础夯实
- 基本语法与数据结构(列表、字典、集合)
- 函数定义与面向对象编程
- 文件操作与异常处理
- 必备库:NumPy、Pandas基础操作
第二阶段:ADS领域专用库学习
…
第三阶段:…
学习目标
熟悉基本语法和数据结构,能熟练使用核心数据结构处理和操作数据。
1 实践
涉及变量:车辆ID(vehicle_id)、传感器名称(sensor_name)、检测置信度(confidence)、激光点云中的一个点的坐标(point_x, point_y, point_z)。
练习1 知识点: list列表
- 创建:创建一个列表,包含5辆车的ID(如[‘car_01’, ‘car_02’, …])。
- 增删改查:模拟动态交通场景:一辆新车加入列表末尾,第一辆车离开列表(被删除)。
- 遍历:遍历并打印所有车辆ID。
- 切片:获取列表中第2到第4辆车的信息。
vehicle_id = ['car_01', 'car_02', 'car_03', 'car_04', 'car_05']
# car_06加入列表末尾
vehicle_id.append('car_06') # append表示在列表末尾添加元素
print(vehicle_id)
['car_02', 'car_03', 'car_04', 'car_05', 'car_06', 'car_06']
# car_01离开列表
vehicle_id.pop(0) # pop(0)表示将列表首个元素弹出,pop()表示将列表末尾元素弹出
print(vehicle_id)
['car_03', 'car_04', 'car_05', 'car_06', 'car_06']
for id in vehicle_id:print(id)
car_03
car_04
car_05
car_06
car_06
vehicle_slice = vehicle_id[1:4]
for id in vehicle_slice:print(id)
car_04
car_05
car_06
练习2 知识点:dict字典
- 创建:创建一辆车的属性字典,包含:id, type(如’sedan’), position(用列表[x, y]表示), speed。
- 增删改查:更新这辆车的速度;为它添加一个is_autonomous(是否自动驾驶)属性;删除type属性。
- 嵌套:创建一个scenario字典,包含:weather(天气)、time_of_day(时间)、vehicles(一个包含多辆车子字典的列表)。
car_attribute = {'id':1, 'type':'sedan', 'position':[1,1], 'speed':60}
print(car_attribute)
{'id': 1, 'type': 'sedan', 'position': [1, 1], 'speed': 60}
# 添加一个key
car_attribute['is_autonomous'] = 1
# 删除一个key
del car_attribute['id']
print(car_attribute)
{'type': 'sedan', 'position': [1, 1], 'speed': 60, 'is_autonomous': 1}
scenario = {'weather': 'sun', 'time_of_day': 'sunday', 'vehicles': [car_attribute]}
print(scenario)
{'weather': 'sun', 'time_of_day': 'sunday', 'vehicles': [{'type': 'sedan', 'position': [1, 1], 'speed': 60, 'is_autonomous': 1}]}
练习3 知识点:set元组
- 去重:一个激光雷达传感器检测到的目标ID列表为[101, 102, 101, 103, 102],使用集合对其进行去重。
- 集合运算:传感器A检测到目标ID集合为{101, 102, 103},传感器B检测到{102, 103, 104},求两个传感器共同看到的目标(交集)、所有看到的目标(并集)、A看到但B没看到的目标(差集)。
# 去重
id_list = [101, 102, 101, 103, 102]
id_set = set(id_list)
print(id_set)
{101, 102, 103}
# 集合运算
id_a = {101, 102, 104}
id_b = {102, 103, 104}
# 交集
inter = id_a & id_b
print(inter)
# 并集
union = id_a | id_b
print(union)
# 差集
diff = id_a - id_b
print(diff)
{104, 102}
{101, 102, 103, 104}
{101}
实战项目:
创建一个包含3辆车的列表。每辆车用一个字典表示,包含id、位置、速度。遍历这个列表,模拟一次时间步长(如1秒)的移动,根据速度更新每辆车的位置(新位置 = 原位置 + 速度),并打印出更新后的所有车辆信息。
car1 = {'id': 1, 'position': [1, 1], 'speed': 10}
car2 = {'id': 2, 'position': [2, 2], 'speed': 20}
car3 = {'id': 3, 'position': [3, 3], 'speed': 30}
cars = [car1, car2, car3]for car in cars:car['position'][0] += car['speed'] * 1
print(cars)
[{'id': 1, 'position': [11, 1], 'speed': 10}, {'id': 2, 'position': [22, 2], 'speed': 20}, {'id': 3, 'position': [33, 3], 'speed': 30}]
2 知识点
2.1 列表
列表元素的追加和弹出
- append():表示在列表末尾追加一个元素
- pop():表示将列表末尾的元素弹出,若为pop(0)则将列表首个元素弹出
列表切片
list2 = list1[1:4],表示对list1做切片,将list1的索引为1到3的元素取出(左开右闭)
2.2 字典
- 键名需要用引号引起
- car_attribute[‘is_autonomous’] = 1,添加一个key
- del car_attribute[‘id’],删除一个key
2.3 元组
- 去重功能:将一个列表转换为元组,则可以对列表中的元素去重
- 逻辑运算:
- 交集:&
- 并集:|
- 差集:-