python-基础 列表 集合 字典 文件处理
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-基础 列表 集合 字典 文件处理相关的知识,希望对你有一定的参考价值。
目录
- 列表、元组操作
- 字典操作
- 集合操作
- 文件操作
1.列表、元组操作
列表是最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作
定义列表
names = ["张三","李四","王麻子"]
通过下标取列表中的数据.下标按0开始计算
names = ["张三","李四","王麻子"] print(names[0]) 张三 print(names[1]) 李四 print(names[2]) 王麻子 注意:也可以倒着取 print(names[-3]) 张三 print(names[-2]) 李四 print(names[-1]) 王麻子
切片:取多个元素(顾头不顾尾.不取尾)
names = ["张三","李四","王麻子","疯子","傻子"] print(names[0:2]) [‘张三‘, ‘李四‘] print(names[:3]) [‘张三‘, ‘李四‘, ‘王麻子‘] print(names[1:]) [‘李四‘, ‘王麻子‘, ‘疯子‘, ‘傻子‘] print(names[0::2]) [‘张三‘, ‘王麻子‘, ‘傻子‘] print(names[::2]) [‘张三‘, ‘王麻子‘, ‘傻子‘]
追加(在最后一位增加)
names = ["张三","李四","王麻子","疯子","傻子"] names.append("SB") print(names)<br>[‘张三‘, ‘李四‘, ‘王麻子‘, ‘疯子‘, ‘傻子‘, ‘SB‘]
插入(随便在什么地方插入)
names = ["张三","李四","王麻子","疯子","傻子"] names.insert(2,"老寒腿") names.insert(5,"大脚") print(names) [‘张三‘, ‘李四‘, ‘老寒腿‘, ‘王麻子‘, ‘疯子‘, ‘大脚‘, ‘傻子‘, ‘SB‘]
修改
names = ["张三","李四","王麻子","疯子","傻子"] names[2] = ("麻子") print(names) [‘张三‘, ‘李四‘, ‘麻子‘, ‘疯子‘, ‘傻子‘]
删除
#指定删除下标对应的 names = ["张三","李四","王麻子","疯子","傻子"] del names[4] print(names) [‘张三‘, ‘李四‘, ‘王麻子‘, ‘疯子‘] #使用remove删除时只能删除第一个出现的 names = ["张三","李四","李四","王麻子","疯子","傻子"] names.remove("李四") print(names) [‘张三‘, ‘李四‘, ‘王麻子‘, ‘疯子‘, ‘傻子‘] #使用pop删除时删除列表中的最后一个值 names = ["张三","李四","李四","王麻子","疯子","傻子"] names.pop() print(names) [‘张三‘, ‘李四‘, ‘李四‘, ‘王麻子‘, ‘疯子‘]
拷贝(此处的拷贝是浅拷贝.意思是只拷贝第一层)
names = ["张三","李四","李四","王麻子","疯子","傻子"] names_copy = names.copy() print(names_copy) [‘张三‘, ‘李四‘, ‘李四‘, ‘王麻子‘, ‘疯子‘, ‘傻子‘]
统计
names = ["张三","李四","李四","王麻子","疯子","傻子"] print("李四出现次数:",names.count("李四")) 李四出现次数: 2
排序
names = ["张三","李四","李四","王麻子","疯子","傻子","3","2"] names.sort() print(names) [‘2‘, ‘3‘, ‘傻子‘, ‘张三‘, ‘李四‘, ‘李四‘, ‘王麻子‘, ‘疯子‘]
#3.0里不同数据类型不能放在一起排序了
获取下标
names = ["张三","李四","李四","王麻子","疯子","傻子","1","3","2"] print("李四的下标是:",names.index("李四")) 李四的下标是: 1 <br>#只返回找到的第一个下标
2.字典操作
字典一种key - value的数据类型。
K怎么定义调用的时候就得怎么调用。
key 定义规则:1.不可变:数字,字符串,元组(可变:列表,字典)。K 唯一(如果K定义重复那么取的时候只取最后一次出现的)
v 定义规则:任意类型
字典:定义符号(),与列表完全一致,唯一不同的是元组内元素不可变
语法:
dic = {name:"Yuhl",age=18,QQ=2878****} 特性:无序 Key必须唯一.
增加
dic = {"name":"Yuhl","age":"18","QQ":"2878****"} dic["Phone"] = "110" print(dic) #增加 {‘Phone‘: ‘110‘, ‘age‘: ‘18‘, ‘QQ‘: ‘2878****‘, ‘name‘: ‘Yuhl‘} dic00 = {"name":{"Yuhl":{"age","QQ"},"QQ":"111"},"age":18} dic00.setdefault("gender","zhang") #增加,随机 print(dic00)
修改
dic = {"name":"Yuhl","age":"18","QQ":"2878****"} dic ["name"] = "Yuhonglin" print(dic) 注意:K值无法进行修改.只能修改value {‘QQ‘: ‘2878****‘, ‘age‘: ‘18‘, ‘name‘: ‘Yuhonglin‘} dic00 = {"name":{"Yuhl":{"age","QQ"},"QQ":"111"},"age":18} dic00.update(name="YUHONGLIN",gender="name") #修改和新增 print(dic00) {‘age‘: 18, ‘gender‘: ‘name‘, ‘name‘: ‘YUHONGLIN‘}
删除
#删除 dic = {"name":"Yuhl","age":"18","QQ":"2878****"} del dic["name"] print(dic) {‘QQ‘: ‘2878****‘, ‘age‘: ‘18‘} #标准删除 dic = {"name":"Yuhl","age":"18","QQ":"2878****"} dic.pop("name") print(dic) {‘age‘: ‘18‘, ‘QQ‘: ‘2878****‘} #随机删除 dic = {"name":"Yuhl","age":"18","QQ":"2878****"} dic.popitem() print(dic) 多试几次有效果
查找
dic = {"name":"Yuhl","age":"18","QQ":"2878****"} print("打印结果是:",dic.get("name")) #获取 打印结果是:Yuhl dic = {"name":"Yuhl","age":"18","QQ":"2878****"} print("打印结果是:",dic["name"]) #与之前的一样 打印结果是:Yuhl dic = {"name":"Yuhl","age":"18","QQ":"2878****"} print("打印结果是:","stu1102" in dic) 打印结果是:False #在字典中有返回True否则返回False dic = {"name":"Yuhl","age":"18","QQ":"2878****"} print("打印结果是:",dic["stu1105"]) #如果一个key不存在会报错. get不会直接返回None
其他方法:
dic00 = {"name":"Yuhl","age":18} dic00.clear() #清空 print(dic00) 答应出来的信息是:{} dic00 = {"name":{"Yuhl":{"age","QQ"},"QQ":"111"},"age":18} dic000002= dic00.fromkeys(["a","b"],1) #快速生成字典 print(dic000002) 自动生成的字典是: {‘b‘: 1, ‘a‘: 1} dic00 = {"name":{"Yuhl":{"age","QQ"},"QQ":"111"},"age":18} for k,v in dic00.items(): print(k,v) #列表打印 print(k[0],v) #列表打印 dic000 = {"name":"Yuhl","age":18,"QQanddianhua":[110,133]} dic00new = dic000.copy() #拷贝.浅拷贝 print(dic00new,"\n",dic000) dic00 = {"name":{"Yuhl":{"age","QQ"},"QQ":"111"},"age":18} dic00["QQanddianhua"]="11222","111111" #拷贝 print("\n",dic00) {‘age‘: 18, ‘QQanddianhua‘: (‘11222‘, ‘111111‘), ‘name‘: {‘QQ‘: ‘111‘, ‘Yuhl‘: {‘age‘, ‘QQ‘}}}
3、集合
作用:去重复 和 关系运算(集合分别有:交集、差集、并集)其他方法:
#交集 python_set = {"alex","lhf","zhangsan","lisi","wangmazi"} linux_set = {"alex","lhf","Yuhl","dsd"} print(python_set&linux_set) #求两个集合里面出现相同的数 print(python_set.intersection(linux_set)) print(python_set&linux_set) print(python_set.intersection(linux_set)) :{‘lhf‘, ‘alex‘} :{‘lhf‘, ‘alex‘} :{‘lhf‘, ‘alex‘} :{‘lhf‘, ‘alex‘} python_set = {"alex","lhf","zhangsan","lisi","wangmazi"} linux_set = {"alex","lhf","Yuhl","dsd"}<br> #并集 print(python_set|linux_set) #去重复 print(python_set.union(linux_set)) #去重复 :{‘lhf‘, ‘Yuhl‘, ‘dsd‘, ‘alex‘, ‘wangmazi‘, ‘lisi‘, ‘zhangsan‘} :{‘lhf‘, ‘Yuhl‘, ‘dsd‘, ‘alex‘, ‘wangmazi‘, ‘lisi‘, ‘zhangsan‘} # #差集 python_set = {"alex","lhf","zhangsan","lisi","wangmazi"} linux_set = {"alex","lhf","Yuhl","dsd"} print(python_set-linux_set) #去掉重复的 print(python_set.difference(linux_set)) print(linux_set-python_set) #去掉重复的 print(linux_set.difference(python_set)) {‘lisi‘, ‘zhangsan‘, ‘wangmazi‘} {‘dsd‘, ‘Yuhl‘} #并集 python_set = {"alex","lhf","zhangsan","lisi","wangmazi"} linux_set = {"alex","lhf","Yuhl","dsd"} print(python_set|linux_set) #去重复(将两个重复数据打印出来.合并在一起) print(python_set.union(linux_set)) #去重复(将两个重复数据打印出来.合并在一起) #d对称差集 python_set = {"alex","lhf","zhangsan","lisi","wangmazi"} linux_set = {"alex","lhf","Yuhl","dsd"} print(python_set^linux_set)#把共有的地方去掉就是对称差集 print(python_set.symmetric_difference(linux_set)) :{‘lisi‘, ‘dsd‘, ‘zhangsan‘, ‘wangmazi‘, ‘Yuhl‘} :{‘lisi‘, ‘dsd‘, ‘zhangsan‘, ‘wangmazi‘, ‘Yuhl‘} s1 = {1,2,3,4,5} s2 = {2,3} #子集 print(s1 <= s2) #s2是s1的子集 print(s1.issubset(s2)) #父集 print(s2 <= s1) #s1是s2的父集 print(s1.issuperset(s2)) s3 = set(["he","l","1","1",1,2,3]) print(s3)
其他方法:
#更新 s1 = {1,2,3} s2 ={"YHL"} s1.update(‘e‘) #插入 s1.update((1,2,3,4,5)) #去重进行写入 s1.update("hello") s1.update(s2) print(s1) #增加 s1 = {1,2,3} s1.add("Hello") #整个加入 print(s1) #删除 s3 = {1,2,3,4,5,6,7,8,9,10} s3.pop() print(s3) #随机删除 s3.remove(10) #指定删除 如果没有会报错 print(s3) s3.discard("a") #不报错的删除 没有也不会报错.有不会报错 print(s3,"------")
4.文件处理
以上是关于python-基础 列表 集合 字典 文件处理的主要内容,如果未能解决你的问题,请参考以下文章
Python基础数据类型(数字字符串布尔列表元组字典集合等)