目录
- 1. 阿里云天池Python教程链接 (2)
- 2. [列表]
- 2.1 基本操作 - 增删改查/复制
- 2.2 组织列表
- 2.3 数字列表的简单统计
- 2.4 其它常用操作
- 3. (元组)
- 4. "字符串"
- 4.1 特殊字符/ 转义符
- 4.2 查询/判定
- 4.3. 分割/拼接
- 4.4 替换
- 4.5 打印格式设置
- 5. {字典}
- 6. {集合}
- 7. 使用 zip 缝合序列
1. 阿里云天池Python教程链接 (2)
2. [列表]
有序,可保存任意类型对象。
2.1 基本操作 - 增删改查/复制
- 创建
使用 range: x = list(range(15, 5, -3)) (不含5)
列表解析: x = [i**2 for i in range(15, 5, -3)]
mixed_list = [1, “Alice”, (2, 3)] - 添加元素: .append(new_data), list1.extend(list2), .insert(0, “new_beginning”)
- 修改元素: list1[2] = “new_data”
- 删除元素:
删除第一个符合条件的值.remove(“old_data”), 使用循环删除多个目标值
del list1[3]
可同时获得被删除的元素.pop() - 清空列表 list1.clear()
- 获取元素: list1[0], list1[-1], list1[1, 10, 2]
- 复制: list2 = list1 v.s. list2 = list1[ : ] v.s. slice
2.2 组织列表
- 排序 cars = [1, 3, 2]
- 永久排序 sort [1, 2, 3], list1.sort(key = len)
- 反序排列 cars.sort(reverse = True) [3, 2, 1]
- 临时排序sorted (cars) (原列表不变)
- 倒序排列 cars.reverse() [2, 3, 1]
2.3 数字列表的简单统计
min(digits), max(digits), sum(digits)
2.4 其它常用操作
- 获取长度 len()
- 切片 player[0:3:1] # (0, 1, 2)
- 成员资格检查 in, not in
- 子字符串计数 .count(obj)
- 索引 .index(x, start, end)
# 加法,乘法
print([1,2,3] + [4,5,6]) # [1,2,3,4,5,6]
print []*3 = [],[],[]
3. (元组)
有序,可保存任意类型对象, 需使用逗号(0,)。
不可更改的列表,可索引和 count, 不可切片。
# 解压
t = (1, 2, 3, 4, 5, 6)
(a, b, *rest, c) = t
print (a, b, c) # 1 2 6
4. “字符串”
一对单引号或双引号之间的字符集合。
字符串不可修改, 其他操作与列表类似。
4.1 特殊字符/ 转义符
换行(\n), TAB(\t), 回车(\r), 反斜杠(\)。换行和 tab 可用来组织输出格式。
4.2 查询/判定
- 查找/确认是否包含: .endwith(), .startwith(), .find(“target_word”)
- 判断是否为数值类型: .isnumeric()
4.3. 分割/拼接
- 分行: splitlines(true)
- 分割: partition(sub), rpartition()
- 按照指定字符分割/去除: split(str="", num), stri1.split(’\n’)
- 拼接 “+”.join(list1), “/”.join(dirs)
4.4 替换
- 替换: replace(old, new)
- 替换: maketrans(intab, outtab), translate(table, deletechars=" ")
4.5 打印格式设置
- 去空白(空格/制表符/换行符): lstrip(), rstrip(), strip()
- 对齐/填充: .center(39), .center(39, “"), ljust(width["”, 100], rjust
- 变换大小写: 首字母大写.title(), 全部小写.lower(), 全部大写.upper()
- 格式化: 'f { } ,最方便🍒🍒
5. {字典}
无序,映射。key为不可变类型,且唯一。
- 创建: dict1 = {a: ‘one’, b: ‘two’, c: ‘three’}
- 添加键和值
初始化 dict.setfault(key, default = None)
直接添加键值对 responses = { }, responses[first] = first_answer - 删除键和值: .del dict1[‘a’], .pop(key), .pop(item), .clear()
- 修改: 根据另一个字典的值来取值: dict2 = dict1.fromkeys(seq, 15)
- 复制/ 更新: .copy(), .update(dict2)
- 获取键和值:
get(“d”, “four”) 当键不存在时,不会报错, 第二个参数用于给新键赋值 (默认值为 None)
遍历键值对
按照特定顺序遍历键
遍历值
遍历值并输出非重复项 (使用集合set)
获取值: dict1 [a]
for key, value in users.items():
print(key, value)
for names in sorted(contact_list.keys()):
print(name.title())
for email in contact_list.values():
print(email)
for email in set(contact_list.values()):
print(email)
6. {集合}
key为不可变类型, 如有重复将被自动过滤。
常用方法:
- 新建:my_set = set()
- 元素个数: len()
- 增加/变更: add(elmnt), .update(set)
- 删除: .remove(item), .discard(value)(不会报错), .pop()
- 集合操作(数学内容): .intersection(set1, set2), set1 & set2, set union(set1, set2), set1 | set2, set.different(set), set1 - set2, set.difference_update(set), set.issubset(set), set1 <= set2, set.isjoint(set)
- 不可变集合: frozenset(range(10)) #frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
7. 使用 zip 缝合序列
names = ["anne", "beth"]
ages = [12, 45]
# 两个列表可以长度不等,短序列用完后将停止缝合
print(list(zip (names, ages))) # [("anne", 12), ("beth", 45)]
for name, age in zip (names, ages):
print(name, "is", age, "year old")