【Python上手】数据结构

列表

  • 有序序列结构
  • 元素可以是不同数据类型
[2, 'Jump Shot', 'Los Angeles Lakers']

# 列表创建
kobe_list = [2, 'Jump Shot', 'Los Angeles Lakers']

# 列表的增减
kobe_list.append('POR')
kobe_list.remove(2)  # 删除列表中已知元素
del kobe_list[-2]  # 删除倒数第二个元素
kobe_list.pop(-2)  # 删除倒数第二个元素
kobe_list.insert(1, 'something')  # 插入元素

# 列表切片
kobe_list[1:10:2]  # 列表名称[起始索引位置:最后索引位置:步长]
kobe_list[-5:-2]  # 反向切片

# 列表推导式
lst = [i**2 for i in range(5)]
lst2 = [i - mean for i in lst]

元组

  • 元素不可变,初始化后不能修改
(2, 'Jump Shot', 'Los Angeles Lakers')

字典

  • 相当于哈希映射
  • 用在需要高速查找的地方
{1: 'Jump Shot',
 2: 'Jump Shot',
 3: 'Jump Shot',
 4: 'Jump Shot',
 5: 'Jump Shot'}

# 字典创建
shot_id = [1,2,3]
shot_zone_area = ['Right Side', 'Left Side', 'Left Side Center']
for key, value in zip(shot_id, shot_zone_area):
	kobe_dict[key] = value

# 字典索引
kobe_dict.get(5)
kobe_dict.get(5,-1)

# 判断键是否存在
key in kobe_dict
kobe_dict.has_key(5)

# 打印键值
kobe_dict.keys()
kobe_dict.values()

# 字典元素删减
del kobe_dict[1]
kobe_dict.pop(2)
kobe_dict.clear()

# 字典推导式
# v_t_r = {ch:i for i, ch in enumerate(lst)}

集合

  • 集合是无序集合,是一组键的集合,不存储值;
  • 不允许存在重复的键,可用于去除重复值;
  • 可以进行数学集合运算,交、并、差等。
set(['Driving Dunk Shot', 'Running Jump Shot', 'Jump Shot'])

# 集合创建
shot_type_set = set()
shot_type_set = {'Driving Dunk Shot', 'Running Jump Shot', 'Jump Shot'}

# 集合运算
shot_type_set1 = {'Driving Dunk Shot', 'Running Jump Shot', 'Jump Shot'}
shot_type_set2 = {'Layup Shot', 'Driving Layup Shot'}
shot_type_set1 | shot_type_set2  # 并集
shot_type_set1 & shot_type_set2  # 交集
shot_type_set1 - shot_type_set2  # 差集
shot_type_set1 ^ shot_type_set2  # 异或
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值