python之路3-元组、列表、字典、集合
1、元组
特点:一旦创建,内容不可修改,又叫只读列表
1 a= ('wang','zhang','zhao') 2 print(a.count('zhao')) 3 print(a.index('wang'))
2、列表
name = ["wang","zhao","zhang"]
切片:取多个元素
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 name = ["wang","zhao","zhang","lili","hao"] 2 print(name[0:3]) 3 print(name[0:-1]) 4 print(name[0:]) 5 print(name[:]) 6 print(name[::2]) 7 运行结果: 8 ['wang', 'zhao', 'zhang'] 9 ['wang', 'zhao', 'zhang', 'lili'] 10 ['wang', 'zhao', 'zhang', 'lili', 'hao'] 11 ['wang', 'zhao', 'zhang', 'lili', 'hao'] 12 ['wang', 'zhang', 'hao']
追加
name.append('张三') print(name) 运行结果: ['wang', 'zhao', 'zhang', 'lili', 'hao', '张三']
插入
1 name.insert(0,'赵四') 2 print(name) 3 运行结果: 4 ['赵四', 'wang', 'zhao', 'zhang', 'lili', 'hao', '张三']
修改
name[0] = 'zhaosi' print(name) 运行结果: ['zhaosi', 'wang', 'zhao', 'zhang', 'lili', 'hao', '张三']
删除
name.remove('zhaosi') print(name) 运行结果: ['wang', 'zhao', 'zhang', 'lili', 'hao', '张三']
del name[1]
print(name)
运行结果:
['wang', 'zhang', 'lili', 'hao', '张三']
扩展
name = ['zhaosi', 'wang', 'zhao', 'zhang', 'lili', 'hao', '张三'] name1 = ['小明','小张','张三']
print(name)
name.extend(name1)
print(name)
运行结果:
['zhaosi', 'wang', 'zhao', 'zhang', 'lili', 'hao', '张三']
['zhaosi', 'wang', 'zhao', 'zhang', 'lili', 'hao', '张三', '小明', '小张', '张三']
拷贝
第一层的不会变,后面的跟着变,列表及字典的第一级修改后,copy的内容不会变,下一级的copy值会跟着变,共享空间,指针
print(name) name1 = name.copy() print(name1) name[0]='赵四' print(name) print(name1) 运行结果: ['zhaosi', 'wang', 'zhao', 'zhang', 'lili', 'hao', '张三'] ['zhaosi', 'wang', 'zhao', 'zhang', 'lili', 'hao', '张三'] ['赵四', 'wang', 'zhao', 'zhang', 'lili', 'hao', '张三'] ['zhaosi', 'wang', 'zhao', 'zhang', 'lili', 'hao', '张三']
统计
print(name.count('zhang'))
排序
name.sort()#正向排序 print(name) name.reverse()#反向排序 print(name)
获取下标
print(name.index('张三')) 运行结果:1
3、字典
特性:无序的;唯一key值,天生去重
student={ 'stu1101':{'name':'张三','age':22}, 'stu1102':{'name':'李四','age':22}, 'stu1103':{'name':'王二','age':25}, }
增加
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 student={ 2 'stu1101':{'name':'张三','age':22}, 3 'stu1102':{'name':'李四','age':22}, 4 'stu1103':{'name':'王二','age':25}, 5 } 6 student['stu1104']='王三' 7 print(student) 8 运行结果: 9 {'stu1101': {'name': '张三', 'age': 22}, 'stu1102': {'name': '李四', 'age': 22}, 'stu1103': {'name': '王二', 'age': 25}, 'stu1104': '王三'}
修改
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 student={ 2 'stu1101':{'name':'张三','age':22}, 3 'stu1102':{'name':'李四','age':22}, 4 'stu1103':{'name':'王二','age':25}, 5 } 6 student['stu1103']['name']='王三' 7 student['stu1101']={'name':'张四','age':23} 8 print(student) 9 运行结果: 10 {'stu1101': {'name': '张四', 'age': 23}, 'stu1102': {'name': '李四', 'age': 22}, 'stu1103': {'name': '王三', 'age': 25}}
删除
pop():删除指定键值对应的数据
popitem():随机删除字典中的一对键和值
del student['stu1101']#删除 print(student) student.pop('stu1101')#删除 print(student)
student.popitem()#随机删除
print(student)
查找
student={ 'stu1101':{'name':'张三','age':22}, 'stu1102':{'name':'李四','age':22}, 'stu1103':{'name':'王二','age':25}, 'stu1105':{'name':'王si','age':26}, 'stu1104':{'name':'王一','age':23}, 'stu1106':{'name':'王sd','age':23}, } print('stu1101' in student) print(student['stu1101'])#key不存在会报错 print(student.get('stu1101'))#key不存在返回None print(student.get('stu1110')) 运行结果: True {'name': '张三', 'age': 22} {'name': '张三', 'age': 22} None
合并更新
student={ 'stu1101':{'name':'张三','age':22}, 'stu1102':{'name':'李四','age':22}, 'stu1103':{'name':'王二','age':25}, 'stu1105':{'name':'王si','age':26}, 'stu1104':{'name':'王一','age':23}, 'stu1106':{'name':'王sd','age':23}, } student2 = { "stu1101":{"name":"wangdawei","age":22,"TEL":1581033}, "stu1110":"zhaoyazhi" } student.update(student2) print(student) 运行结果: {'stu1101': {'name': 'wangdawei', 'age': 22, 'TEL': 1581033}, 'stu1102': {'name': '李四', 'age': 22}, 'stu1103': {'name': '王二', 'age': 25},
'stu1105': {'name': '王si', 'age': 26}, 'stu1104': {'name': '王一', 'age': 23}, 'stu1106': {'name': '王sd', 'age': 23}, 'stu1110': 'zhaoyazhi'}
其他
print(student.keys())#返回可遍历的KEY值 print(student.values())#返回可遍历的value值 print(student.items())#返回可遍历的KEY,value值
for key in student:
print(key,info[key])
for k,v in student.items(): #会先把dict转成list,数据里大时莫用
print(k,v)
4、集合
集合是一个无序的,数据不重复的数据组合,作用如下:
- 去重,把一个列表变成集合就自动去重了
- 测试两组数据之间的交集、并集、差集等关系
定义:
a = {1,3,5,7,10} b = {2,3,4,5,6,8}
#交集
print(a & b)
print(a.intersection(b))
print(a.intersection_update(b)) #等价于a = a.intersection(b)
print(a)
#差集
a = {1,3,5,7,10}
b = {1,3,5}
print(a-b)
print(a.difference(b))
运行结果:
{10, 7}
{10, 7}
#并集
print(a | b)
print(a.union(b))
运行结果:
{1, 2, 3, 4, 5, 6, 7, 8, 10}
{1, 2, 3, 4, 5, 6, 7, 8, 10}
#对称差集
print(a ^ b)
print(a.symmetric_difference(b))#去掉公共部分
运行结果:
{1, 2, 4, 6, 7, 8, 10}
{1, 2, 4, 6, 7, 8, 10}
5、join()函数
join(): 连接字符串数组。将字符串、元组、列表中的元素以指定的字符(分隔符)连接生成一个新的字符串
语法: 'sep'.join(seq)
参数说明
sep:分隔符。可以为空
seq:要连接的元素序列、字符串、元组、字典
上面的语法即:以sep作为分隔符,将seq所有的元素合并成一个新的字符串
返回值:返回一个以分隔符sep连接各个元素后生成的字符串
student={ 'stu1101':{'name':'张三','age':22}, 'stu1102':{'name':'李四','age':22}, 'stu1103':{'name':'王二','age':25}, } list=['wang','zhao'] print(''.join(list)) print('.'.join(list)) print('.'.join(student)) 运行结果: wangzhao wang.zhao stu1101.stu1102.stu1103