【ADS-1】【python基础-2】基本语法与数据结构(列表、字典、集合)

目录

  • [1 实践](#1 实践)
  • [2 知识点](#2 知识点)
    • [2.1 列表](#2.1 列表)
    • [2.2 字典](#2.2 字典)
    • [2.3 元组](#2.3 元组)

前言

第一阶段:Python基础夯实

  1. 基本语法与数据结构(列表、字典、集合)
  2. 函数定义与面向对象编程
  3. 文件操作与异常处理
  4. 必备库:NumPy、Pandas基础操作

第二阶段:ADS领域专用库学习

​​...
第三阶段:...

学习目标

熟悉基本语法和数据结构,能熟练使用核心数据结构处理和操作数据。

1 实践

涉及变量:车辆ID(vehicle_id)、传感器名称(sensor_name)、检测置信度(confidence)、激光点云中的一个点的坐标(point_x, point_y, point_z)。

练习1 知识点: list列表

  1. 创建:创建一个列表,包含5辆车的ID(如'car_01', 'car_02', ...)。
  2. 增删改查:模拟动态交通场景:一辆新车加入列表末尾,第一辆车离开列表(被删除)。
  3. 遍历:遍历并打印所有车辆ID。
  4. 切片:获取列表中第2到第4辆车的信息。
python 复制代码
vehicle_id = ['car_01', 'car_02', 'car_03', 'car_04', 'car_05']
python 复制代码
# car_06加入列表末尾
vehicle_id.append('car_06') # append表示在列表末尾添加元素
print(vehicle_id)
复制代码
['car_02', 'car_03', 'car_04', 'car_05', 'car_06', 'car_06']
python 复制代码
# car_01离开列表
vehicle_id.pop(0) # pop(0)表示将列表首个元素弹出,pop()表示将列表末尾元素弹出
print(vehicle_id)
复制代码
['car_03', 'car_04', 'car_05', 'car_06', 'car_06']
python 复制代码
for id in vehicle_id:
    print(id)
复制代码
car_03
car_04
car_05
car_06
car_06
python 复制代码
vehicle_slice = vehicle_id[1:4]
for id in vehicle_slice:
    print(id)
复制代码
car_04
car_05
car_06

练习2 知识点:dict字典

  1. 创建:创建一辆车的属性字典,包含:id, type(如'sedan'), position(用列表x, y表示), speed。
  2. 增删改查:更新这辆车的速度;为它添加一个is_autonomous(是否自动驾驶)属性;删除type属性。
  3. 嵌套:创建一个scenario字典,包含:weather(天气)、time_of_day(时间)、vehicles(一个包含多辆车子字典的列表)。
python 复制代码
car_attribute = {'id':1, 'type':'sedan', 'position':[1,1], 'speed':60}
print(car_attribute)
复制代码
{'id': 1, 'type': 'sedan', 'position': [1, 1], 'speed': 60}
python 复制代码
# 添加一个key
car_attribute['is_autonomous'] = 1
# 删除一个key
del car_attribute['id']
print(car_attribute)
复制代码
{'type': 'sedan', 'position': [1, 1], 'speed': 60, 'is_autonomous': 1}
python 复制代码
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元组

  1. 去重:一个激光雷达传感器检测到的目标ID列表为101, 102, 101, 103, 102,使用集合对其进行去重。
  2. 集合运算:传感器A检测到目标ID集合为{101, 102, 103},传感器B检测到{102, 103, 104},求两个传感器共同看到的目标(交集)、所有看到的目标(并集)、A看到但B没看到的目标(差集)。
python 复制代码
# 去重
id_list = [101, 102, 101, 103, 102]
id_set = set(id_list)
print(id_set)
复制代码
{101, 102, 103}
python 复制代码
# 集合运算
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秒)的移动,根据速度更新每辆车的位置(新位置 = 原位置 + 速度),并打印出更新后的所有车辆信息。

python 复制代码
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 列表

列表元素的追加 和弹出

  1. append():表示在列表末尾追加一个元素
  2. pop():表示将列表末尾的元素弹出,若为pop(0)则将列表首个元素弹出

列表切片

list2 = list11:4,表示对list1做切片,将list1的索引为1到3的元素取出(左开右闭)

2.2 字典

  1. 键名需要用引号引起
  2. car_attribute'is_autonomous' = 1,添加一个key
  3. del car_attribute'id',删除一个key

2.3 元组

  1. 去重功能:将一个列表转换为元组,则可以对列表中的元素去重
  2. 逻辑运算:
    1. 交集:&
    2. 并集:|
    3. 差集:-
相关推荐
the局外人24 分钟前
Python 实战检索优化:BM25、向量与重排
数据库·后端·python
__underline__27 分钟前
冷门Python知识点006篇:Python 类体里的名字,能被"凭空造出来"
后端·python
用户0332126663671 小时前
使用 Python 轻松移除 Excel 文档中的重复行
python·excel
LiaoWL1231 小时前
从零搭一个 RAG 知识库(二):换上语义 Embedding——BGE 实测与阈值重校准
python·大模型·numpy·embedding·向量检索·rag
Dmz丶2 小时前
前端转 AI 100 天 Day 48:向量数据库——100 万向量的毫秒级检索
python
青少儿编程课堂2 小时前
蓝桥杯·全国青少年信息素养大赛·电子学会Python/C++考级:中国剩余定理解析
c++·python·算法·bfs·信息学竞赛
菜鸟~noob2332 小时前
[AI]间章:系列走到这里,聊聊后面的规划、造卡需要的软件平台,以及为什么我要先暂停更新
人工智能·python·fpga开发·django·pygame
TomEval12 小时前
【测AI】第26篇:线上模型监控与告警体系 — 测试不是终点
人工智能·python·功能测试·ai
prince_zxill13 小时前
PEP 751 pylock.toml 实测:pip 26.3 装 6 个包版本全钉死,但 [web] 选不中任何东西
python·pip·依赖管理·pep 751
Thanks_ks14 小时前
Python 的垃圾回收——引用计数撑起日常,分代回收收拾残局
python·内存管理·垃圾回收·循环引用·引用计数·gc 模块·weakref