1.列表操作
1.1 遍历
if __name__ == "__main__": name_list = ["张三", "李四", "王五", "老鳖"] for name in name_list: print(name)
for k, v in enumerate(name_list): print("k:{} v:{}".format(k, v))
|
1.2 统计
if __name__ == "__main__": number_list = [1, 2, 3, 4, 5, 5, 4, 2] print("统计元素个数:", len(number_list)) print("统计元素5出现的个数:", number_list.count(5)) print("列表中最大的元素:", max(number_list)) print("列表中最小的元素:", min(number_list)) print("元素之和:", sum(number_list))
|
1.3 去重(转集合)
if __name__ == "__main__": list_var = [1, 2, 3, 3, 2, 1] set_var = set(list_var) print("set_var:", set_var) print("set_var type:", type(set_var))
|
1.4 追加元素
if __name__ == "__main__": list_var = [1, 3, 4, 5] list_var.append(100) print("末尾追加元素 list_var: ", list_var) list_var.insert(1, 2) print("指定位置追加元素 list_var: ", list_var) list_var2 = [6, 7, 8] list_var.extend(list_var2) print("追加一个集合 list_var: ", list_var)
|
1.5 删除元素
if __name__ == "__main__": list_var = [1, 5, 4, 2, 5, 7, 3, 8, 9] print("原始元素: ", list_var) list_var.pop() print("删除最后一个元素后: ", list_var) list_var.pop(2) print("删除索引=2的元素后: ", list_var) list_var.remove(5) print("删除值=5的元素后: ", list_var) list_var.clear() print("清空整个列表后: ", list_var)
|
@注意:删除之前都需要判断,被删的元素或索引是否存在,否则会报异常。
1.6 反转和排序
if __name__ == "__main__": list_var = [1, 5, 4, 3, 7] print("原始元素: ", list_var) list_var.reverse() print("反向排列: ", list_var) list_var.sort(reverse=False) print("正序排序: ", list_var) list_var.sort(reverse=True) print("降序排序: ", list_var)
|
1.7 传参和复制
def changeList(names): for k, v in enumerate(names): names[k] = "{}|".format(v) return names
if __name__ == "__main__": name_list = ["张三", "李四", "王五", "老鳖"] print("修改前,name_list:", name_list) changeList(name_list) print("修改后,name_list:", name_list)
|
从上面例子可以看出,列表属于引用数据类型,即在函数内修改,函数外面也会生效,所以当把列表当参数传时特别要注意,如果不想函数外也跟着变化,可以传副本。
if __name__ == "__main__": name_list = ["张三", "李四", "王五", "老鳖"] print("原列表内存地址:", id(name_list)) name_copy = name_list.copy() print("使用copy复制列表:", id(name_copy)) name_copy2 = name_list[:] print("使用:复制:", id(name_copy2))
|
2. 集合操作
@注: 集合也支持 clear、pop、copy、remove方法
2.1 追加和删除
if __name__ == "__main__": set1 = {1, 2, 3} set1.add(10) print("添加元素10后:", set1) print("删除元素100后:", set1.discard(100)) print("删除元素10后:", set1.discard(1))
|
discard
和remove
函数功能一样,都是删除指定元素;唯一的区别是: 如果去除的元素不存在,remove()会引发一个KeyError,而discard()不会返回任何异常。
2.2 求并集
if __name__ == "__main__": set1 = {1, 3, 5, 7} set2 = {1, 2, 4, 6, 8, 10} print("使用符号 | 求并集:", set1 | set2) print("使用函数 union 求并集:", set1.union(set2))
|
2.3 求交集
if __name__ == "__main__": set1 = {1, 3, 5, 7, 8} set2 = {1, 2, 4, 6, 8, 10} print("使用符号 & 求交集:", set1 & set2) print("使用函数 intersection 求交集:", set1.intersection(set2))
|
2.4 求差集
if __name__ == "__main__": set1 = {1, 2, 3} set2 = {1, 2, 4} print("set1求差集,使用符号 - 求差集:", set1 - set2) print("set1求差集,使用函数 difference 求交集:", set1.difference(set2)) print("set2求差集,使用符号 - 求差集:", set2 - set1) print("set2求差集,使用函数 difference 求交集:", set2.difference(set1))
|
2.5 求对称差集
if __name__ == "__main__": set1 = {1, 2, 3} set2 = {1, 2, 4} print("set1求对称差集,使用符号 - 求差集:", set1 ^ set2) print("set1求对称差集,使用函数 difference 求交集:", set1.symmetric_difference(set2))
|
2.6 判断子集和超集
if __name__ == "__main__": set1 = {1, 2, 3} set2 = {4, 5, 6} set3 = {1, 3, 5} print("set1和set2有交集?:", set1.isdisjoint(set2)) print("set1和set3有交集?:", set1.isdisjoint(set3))
set4 = {1, 2} print("set4是set1的子集?:", set4.issubset(set1)) print("set4是set2的子集?:", set4.issubset(set2))
print("set1是set4的超集?:", set1.issuperset(set4)) print("set2是set4的超集?:", set2.issuperset(set4))
|
3.字典
3.1 转字符串
if __name__ == "__main__": map_v1 = {"name": "张三", "age": 20, "sex": "男"} print("转字符串: ", str(map_v1))
|
3.2 遍历
if __name__ == "__main__": map_v1 = {"name": "张三", "age": 20, "sex": "男"} for k, v in map_v1.items(): print("k: {} v:{} ".format(k, v))
|
3.3 key是否存在
if __name__ == "__main__": map_v2 = {"name": "张三", "age": 23, "sex": "男", "home": "北京"} print("key = name 是否存在: ", "name" in map_v2) print("key = city 是否存在: ", "city" in map_v2)
|
3.4 获取key和val
if __name__ == "__main__": map_v1 = {"name": "张三", "age": 20, "sex": "男"} keys = map_v1.keys() print("所有key:{} 类型: {}".format(keys, type(keys))) print("转成list,在访问: ", list(keys)[0]) values = map_v1.values() print("所有values:{} 类型: {}".format(values, type(values)))
|
3.5 重置map值
if __name__ == "__main__": map_v1 = {"语文": 60, "英语": 40, "数学": 73} keys = map_v1.keys() newMap = map_v1.fromkeys(keys, 100) print("重置map_v1值为100后: ", newMap) list_v1 = ["张三", "李四", "王五"] age_map = {}.fromkeys(list_v1, 18) print("基于列表创建map: ", age_map)
|
3.6 字典合并
if __name__ == "__main__": map_v1 = {"语文": 60, "英语": 40, "数学": 73} map_v2 = {"化学": 50, "英语": 66} map_v1.update(map_v2) print("map_v1: ", map_v1)
|
用字典: map_v2 更新 map_v1,如果key一样,则用map_v2覆盖map_v1。
3.7 删除元素
if __name__ == "__main__": map_v1 = {"语文": 60, "英语": 40, "数学": 73, "化学": 50} map_v1.pop("语文") print("删除 key=语文:", map_v1)
map_v2 = {"name": "张三", "age": 23, "sex": "男", "home": "北京"} res = map_v2.popitem() print("删除最后一对键和值->并返回: ", res) print("删除最后map_v2: ", map_v2)
|
3.8 读取key
if __name__ == "__main__": map_v2 = {"name": "张三", "age": 23, "sex": "男", "home": "北京"} print("key存在,key:name value: {}".format(map_v2.get("name", ""))) print("key不存在,key:city value: {} ".format(map_v2.get("city", "未知")))
|