Python基础day-4[dict,set,bool]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python基础day-4[dict,set,bool]相关的知识,希望对你有一定的参考价值。
布尔类型:
True和False
所有的数据类型自带布尔值,只有0,None,空的布尔值为False
字典dict:
特性:dict中使用key和对应的value来存储数据,dict不像 list那样,数据越多查询越慢.dict中key必须是不可变类型,或者说必须是可hash类型.value的值是可以改变的,可以是任意类型的数据.字典的取值是无序的.无法通过索引取值.
定义dict:
d = {key:value,key:value,key:value}
d = {‘name‘:‘abc‘,‘age‘:18,1:‘id‘}
新增:
d = {}
d[‘x‘] = 1
取值: 通过key取值
print(d[‘name‘])
print(d[1])
循环:
for i in d:
print(i) #利用for循环取直接取字典内容,只能取出字典的key
print(d[i]) #利用取出的key,打出对应的value
嵌套使用: list和字典可以互相套,list中可以有字典,字典的value也可以是列表,注意:list是不可hash类型,也就说不能作为key
user_info=[ #定义一个列表,列表中使用字典存储用户名和密码
{‘username‘:‘egon‘,‘password‘:‘123‘},
{‘username‘:‘alex‘,‘password‘:‘alex3714‘},
{‘username‘:‘yuanhao‘,‘password‘:‘sb123‘},
]
for item in user_info: #for循环提取list中的dict
print(item[‘username‘],item[‘password‘]) #打印dict中的 username和password
成员运算:
d = {‘x‘:1,‘u‘:2}
print(‘x‘ in d)
print(1 in d.values())
dict的常用操作:
d.clear():清空字典的所有内容
d = {‘name‘:‘abc‘,‘age‘:18,1:‘id‘}
d.clear()
print(d)
D:\Python\Python36-32\python.exe E:/Python/tmp.py
{}
Process finished with exit code 0
d.get():带返回值的取值方式,当取出不存的值时会返回None 不会报错
d = {‘name‘:‘abc‘,‘age‘:18,1:‘id‘}
print(d.get(‘abc‘))
D:\Python\Python36-32\python.exe E:/Python/tmp.py
None
Process finished with exit code 0
d.items():以元组的方式显示键值对,for循环时可以给两个变量,同时取出key和value
d = {‘name‘:‘abc‘,‘age‘:18,1:‘id‘}
for k,v in d.items():
print(k,v)
print(‘==================‘)
for i in d.items():
print(i)
D:\Python\Python36-32\python.exe E:/Python/tmp.py name abc age 18 1 id ================== (‘name‘, ‘abc‘) (‘age‘, 18) (1, ‘id‘) Process finished with exit code 0
d.keys and d.values:指定提取全部的key或者value
d = {‘name‘:‘abc‘,‘age‘:18,1:‘id‘}
print(d.keys())
print(d.values())
D:\Python\Python36-32\python.exe E:/Python/tmp.py dict_keys([‘name‘, ‘age‘, 1]) dict_values([‘abc‘, 18, ‘id‘]) Process finished with exit code 0
d.pop():pop是带返回值的,精确删除某个key
d = {‘name‘:‘abc‘,‘age‘:18,1:‘id‘}
print(d.pop(‘age‘)) #pop()必须给予一个key,否则报错
print(d)
print(d.pop(‘abc‘,‘没有abc那个key‘)) #由于pop带返回值,所以可以指定返回信息,来去除错误信息.不指定返回值,当删除一个不存在的key时会报错.
D:\Python\Python36-32\python.exe E:/Python/tmp.py 18 {‘name‘: ‘abc‘, 1: ‘id‘} 没有abc那个key Process finished with exit code 0
d.popitem():随机删除一组key和对应的value
d = {‘name‘:‘abc‘,‘age‘:18,1:‘id‘}
d.popitem()
print(d)
D:\Python\Python36-32\python.exe E:/Python/tmp.py {‘name‘: ‘abc‘, ‘age‘: 18} Process finished with exit code 0
d.setdefault():向dict中添加key和对应的value,如果key已存在则不添加(也不会报错.)
d = {‘name‘:‘abc‘,‘age‘:18,1:‘id‘}
d.setdefault(‘aaa‘,111)
print(d)
d.setdefault(‘name‘,‘abc‘)
print(d)
d.setdefault(‘name‘,‘abc123‘)
print(d)
D:\Python\Python36-32\python.exe E:/Python/tmp.py {‘name‘: ‘abc‘, ‘age‘: 18, 1: ‘id‘, ‘aaa‘: 111} {‘name‘: ‘abc‘, ‘age‘: 18, 1: ‘id‘, ‘aaa‘: 111} {‘name‘: ‘abc‘, ‘age‘: 18, 1: ‘id‘, ‘aaa‘: 111} Process finished with exit code 0
几种新建字典的方式:
d1={} d2=dict() print(d1,d2) d3=dict(x=1,y=2,z=3) print(d3) d4=dict({‘x‘:1,‘y‘:2,‘z‘:3}) print(d4)
d5=dict([(‘x‘,1),(‘y‘,2),(‘z‘,3)])
print(d5)
D:\Python\Python36-32\python.exe E:/Python/tmp.py {} {} {‘x‘: 1, ‘y‘: 2, ‘z‘: 3} {‘x‘: 1, ‘y‘: 2, ‘z‘: 3}
{‘x‘: 1, ‘y‘: 2, ‘z‘: 3} Process finished with exit code 0
d.fromkeys(): 创建的dict的格式化,前面所有的key匹配同一个value
d={}.fromkeys([‘name‘,‘age‘],None)
print(d)
D:\Python\Python36-32\python.exe E:/Python/tmp.py {‘name‘: None, ‘age‘: None} Process finished with exit code 0
d.update():更新列表,没有的key就添加,key如果存在就更新value
d = {‘name‘:‘abc‘,‘age‘:18,1:‘id‘}
d1 = {‘name‘:‘ac‘,1:‘i‘}
d1.update(d)
print(d1)
D:\Python\Python36-32\python.exe E:/Python/tmp.py {‘name‘: ‘abc‘, 1: ‘id‘, ‘age‘: 18} Process finished with exit code 0
集合set:
特性:集合内的元素必须是唯一的,集合内的元素必须是可hash的,也就是不可变类型,集合是无序的
定义集合:
s = {‘abc‘,1,‘aaa‘,(1,2),‘name‘}
集合的作用:
1.去重 #因为集合的元素是唯一的所以可用于去除重复的元素.
2.关系运算:
python_s={‘egon‘,‘alex‘,‘abc‘,‘wang‘}
linux_s={‘alex‘,‘abc‘,‘aaa‘,‘bbb‘}
# 取共同部分:交集
print(python_s & linux_s)
#取老男孩所有报名学习的学生:并集
print(python_s | linux_s)
#取只报名了python课程的学生:差集
print(python_s - linux_s)
# 取只报名了linux课程的学生:差集
print(linux_s - python_s)
# 取没有同时报名python和linux课程的学:对称差集
print(linux_s ^ python_s)
D:\Python\Python36-32\python.exe E:/Python/tmp.py {‘abc‘, ‘alex‘} {‘wang‘, ‘bbb‘, ‘abc‘, ‘aaa‘, ‘egon‘, ‘alex‘} {‘egon‘, ‘wang‘} {‘aaa‘, ‘bbb‘} {‘aaa‘, ‘egon‘, ‘wang‘, ‘bbb‘} Process finished with exit code 0
集合的操作:
关系运算
python_s={‘egon‘,‘alex‘,‘abc‘,‘wang‘}
linux_s={‘alex‘,‘abc‘,‘aaa‘,‘bbb‘}
print(python_s.intersection(linux_s)) # 交集:python_s & linux_s
print(python_s.union(linux_s)) #并集:|
print(python_s.difference(linux_s)) #python_s-linux_s
print(python_s.symmetric_difference(linux_s)) # 对称差集,python_s ^ linux_s
D:\Python\Python36-32\python.exe E:/Python/tmp.py {‘abc‘, ‘alex‘} {‘wang‘, ‘egon‘, ‘abc‘, ‘alex‘, ‘bbb‘, ‘aaa‘} {‘wang‘, ‘egon‘} {‘bbb‘, ‘wang‘, ‘egon‘, ‘aaa‘} Process finished with exit code 0
s.difference_update():差集更新
python_s={‘egon‘,‘alex‘,‘abc‘,‘wang‘}
linux_s={‘alex‘,‘abc‘,‘aaa‘,‘bbb‘}
python_s.difference_update(linux_s)
print(python_s)
D:\Python\Python36-32\python.exe E:/Python/tmp.py {‘wang‘, ‘egon‘} Process finished with exit code 0
s.update():更新,如果没有添加
s1={‘a‘,1}
s2={‘a‘,‘b‘,2}
s1.update(s2)
print(s1)
D:\Python\Python36-32\python.exe E:/Python/tmp.py {1, 2, ‘b‘, ‘a‘} Process finished with exit code 0
s.add():添加一个元素
s={‘a‘,1}
s.add(‘abc‘)
print(s)
D:\Python\Python36-32\python.exe E:/Python/tmp.py {1, ‘a‘, ‘abc‘} Process finished with exit code 0
s.discard():删除一个指定的元素,删除不存在元素时不会报错
s={‘a‘,1}
s.discard(‘a‘)
print(s)
D:\Python\Python36-32\python.exe E:/Python/tmp.py {1} Process finished with exit code 0
s.remove():删除一个指定元素,删除不存在的元素时会报错
s={‘a‘,1}
s.remove(‘a‘)
print(s)
D:\Python\Python36-32\python.exe E:/Python/tmp.py {1} Process finished with exit code 0
s.pop():随机删除一个元素
s={‘a‘,2,3,4,5,6,7,8,9}
print(s.pop())
print(s)
D:\Python\Python36-32\python.exe E:/Python/tmp.py a {2, 3, 4, 5, 6, 7, 8, 9} Process finished with exit code 0
s.issubset():判断是否是子集
s.issuperset():判断是否是父集
s1={1,2,}
s2={1,2,3}
print(s1.issubset(s2))
print(s2.issuperset(s1))
D:\Python\Python36-32\python.exe E:/Python/tmp.py
True
True
Process finished with exit code 0
s.isdisjoint():没有交集返回True
s1={‘a‘,‘b‘}
s2={1,2,3}
print(s1.isdisjoint(s2))
D:\Python\Python36-32\python.exe E:/Python/tmp.py
True
Process finished with exit code 0
以上是关于Python基础day-4[dict,set,bool]的主要内容,如果未能解决你的问题,请参考以下文章