集合字典,运算符

##集合字典运算符  特点:无序性,唯一性。(用大括号来表示)

se = {1,2,2,4}
se
{1,2,4}  #把重复的去掉了  体现唯一性

se = {1,'a',2,'b'}   #体现无序性
se
{'b',1,2,'a'}   


se={1,2,(1,2)}
len(se)
3

 案例分析:

# 分析这两种定义,哪一个会报错?
s1 = {1, ['a', 'b', 'c']} 
s2 = {1, ('a', 'b', 'c')}

第一种会报错。TypeError: unhashable type: 'list'

因为集合中的元素要符合不可变这个规则, 列表可变,不符合规则,报错了。

第二种,元组不可变,符合规则,所以解释器不会跟你叫劲。

 

se = {1,2,}

type(se)

<class 'set'>

#定义一个空集合
se
=set() se set() type(se) <class 'set'> se={} type(se) <class 'dict'>

集合的一些运算se1 = {1,2,3};se2 = {2,3,4}


se1|se2         #并集:shift加上面一个键打出来

[1,2,3,4]


se1&se2 #交集,取重复部分
{2,3}


se1-se2 #差集,减去两个集合重复部分
{1}

se2-se1
{4}


se1^se2 #取各自集合独立部分元素(与非集)
{1,4}


se={1,2} #集合是可变的,(还有列表)也是可变的,添加单个元素
se.add(3)
{1,2,3}

se.update(4) #应该放可迭代的参数
报错


se.update('4')
{1,2,3,'4'}


se.update('456') #添加可迭代对象
se
{1,2,3,'5','4','6'}


se.remove('5') #无序性,要指定位置移除
se
{1,2,3,4,'4','6'}

se.pop() #因为集合是无序性,所以POP出来的是随机移除


list(se) #转换成列表和元组就可以索引
[2,3,'4','6']

tuple(se)
(2,3,'4','6')

 

字典   #python中唯一一个映射类型

li = ['w','l','x']
l2 = [123,456,789]

li[2]
'x'
li.index('x')
2
l2[2]
789

l2[li.index('x')]
789

 

di = {'w':123,'l':456,'x':789}   #键值对  dict(key:value)

di=dict()
di
{}

di=dict(w=123)
di
{'w':123}

di=dict(1=123)
报错

di=dict(_1=123)  #要符合变量命名原则


di={'w':123,'l':456,'x':789}
di{'x'}
789

di['x']=987   #字典值进行修改
di
{'w':123,'l':456,'x':987}

di={'w':123,'l':456,'x':789,'x':199} key键是唯一不可变,如果有相同会去重
di.clear()
{}

di.fromkeys(['a','b','c'])
{'a':None,'b':None,'c':None}


di.fromkeys(['a','b','c'],123) 用给定的键,建立新的字典,每一个键默认对应None(可以定义)
'c': 123, 'a': 123, 'b': 123}


help(di.fromkeys) #可迭代类型

di={'l':456,'x':987,'w':123}
di.get('w')
123
di.get('r')
不会报错,也没反应

di.get('r','我不在这里') #di.get('r','我不在这里') 取值,存在就返回对应值,不存在就默认返回none(自定义)
'我不在这里'


di.items()
dict_items([('x', 987), ('l', 456), ('w', 123)])


list(di.items()) #list(di.items())查看字典的每一项
[('x', 987), ('l', 456), ('w', 123)]


di.keys() #查看字典的所有键
dict_keys(['x', 'l', 'w'])

di.values() #查看字典的值
dict_values([987, 456, 123])


di.pop() #报错,原因:至少要一个参数

di.pop('w')
123
di
{'1':456,'x':987}


di.pop('w','x')
123
di
{'1':456,'x':987}

di.pop('w','x')
'x'
di
{'l':456,'x':987}


di.pop('w',123) #指定键,弹出对应值,如果键不存在,可以自定义返回值
123


di.pop('w','x')
'x'
di
['1':456,'x':987}

di.pop('w','x')
'x'
di
{'1':456,'x':987}


di.popitem() #把冒号变成了逗号,以元组形式返回出来,随机删除某一项
('x',987)

di.setdefault('l')
456
di
{'1':456}

di.setdefault('w',123) #类似get,存在就返回值,不存在就更新进去,对应的值默认为None(也可以自定义加进去)


di.setdefault('x')
di
{'1':456,'x':None}
di.setdefault('w',123)
123
di
{'1':456,'x':None,'w':123}


#di.update用法
di={'1':456,'x':None,'w':123} #将一个字典内容di2添加并更新覆盖到原来的字典(di)中!
di2={'p':234,'x':123}
di.update(di2)
di
{'1':456,'x':123,'w':123,'p':234}

 

 

##集合字典运算符
#set 集合
se = {1,2,'a','b'}
#特点 无序性 唯一性
se1 = {1,1,2,3,4,4}

#se3 = {1,2,[3,4]} 报错
hash((1,2))

#定义一个空集合
se3 = set()
#se4 = {} 字典


#集合的运算
2 in se #属于
#se <= se2 包含

se={1,2,3,4};se2={3,4,5,6}

se & se2 #交集 取俩个集合重复的部分
se | se2 #并集
se - se2 #差集 前面的集合减去后面的集合元素重复的部分
se ^ se2 #与非集 取俩个集合各自独有的元素


##集合的方法
se.add(5) #添加单个元素
se.update([1,2,3]) #添加可迭代对象
se.remove(1) #指定移除


##字典 无序 键唯一不可变()
di={'a':111,'b':222,'c':333,'d':444}
di2 = dict(_a=111) # a符合变量的命名规则

#取值 修改
di['c']=3

#方法
#di.fromkeys(['a','b'],123)

di.get('e','wobuzai') #在就返回对应值,不在可以自定义返回值

di.items() #返回每一项,包装成元组 list()

di.keys()

di.values()


di.setdefault('d',444) #类似get 但是能改变字典


di.update({'a':123,'f':555})#有就更新值,没有就添加

 

 总结:元组,字符串,数值类型不可变,其它都可变

补充:

di = {'x':[123,456]}             #key唯一的,value不唯一
di
{'x':[123,456]}
di['x']
[123,456]

di['w']=234
di
{'x':[123,456],'w':234}


#运算符
a=1;b=2
a==b #判断是否相同
False

a!=b #a不等于b
True


a is b
False


a==1
True
a==1 and ==3 #要两个条件都满足,返回True
False



a==1 or b==3 #满足其中一个返回True
True


b==3
False

not b==3
True


a==1 and not b==3
True

 

posted @ 2018-03-16 14:31  追梦长胜  阅读(229)  评论(0)    收藏  举报