码迷,mamicode.com
首页 > 编程语言 > 详细

Python 基础之集合相关操作与函数和字典相关函数

时间:2019-05-11 21:41:46      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:判断   middle   操作   集合   ==   版本   更新   strong   ===   

:集合相关操作与相关函数

1.集合相关操作(  )
#intersection() 交集
set1 = {"one","two","three"}
set2 = {"four","five","one"}
res = set1.intersection(set2)
print(res)
res = set1 & set2
print(res)

#difference() 差集
set1= {"one","two","three","four","five"}
set2 = {"six","seven","eigth","four"}
res = set1.difference(set2)
print(res)
res = set1 - set2
print(res)

#unoion() 并集
set1= {"one","two","three","four","five"}
set2 = {"six","seven","eigth","four"}
res = set1.union(set2)
print(res)
res = set1 | set2
print(res)

#symmrtric_difference() 对称差集(补集情况涵盖在其中)
set1= {"one","two","three","four","five"}
set2 = {"six","seven","eigth","four"}

res = set1.symmetric_difference(set2)
print(res)
res = set1 ^ set2
print(res)

#issubset() 判断是否子集
set1 = {"one","two","three"}
#set2 = {"王文","王俊文","黄文"}
set2 = {"one"}
res = set2.issubset(set1)  #可以完全一样
print(res)
#真子集: 2个集合不是完全一样的
res = set2 <= set1

print(res)

# res = set1.issuperset(set2)
# #也就是issubset()issuperset()在两个集合元素相同的情况下都是Ture
# print(res)
#issuperset() 判断是否是父集
set1 = {"one","two","three"}
set2 = {"one"}
res = set1.issuperset(set2)
print(res)
res = set1 >= set2 #>= 是要用等于的
print(res)


#isdisjoint() 检测两集合是否相交 不相交Ture 相交False
set1 = {"one","two","three"}
set2 = {"one"}
res = set1.isdisjoint(set2)
print(res)

2.集合相关函数

#(1)
#add() 相集合中添加数据
print("==========增============")
setvar = {"one","two","three"}
setvar.add("four")
print(setvar)

print("==========update============")
#update() 迭代着增加 update (可迭代性数据)
setvar.update(["one","two"])
print(setvar)
print("==========删============")
#(2)删除
#clear() 清空集合
setvar = {"one","two","three"}
setvar.pop()
print(setvar)

#pop() 随机删除集合的一个数据
setvar = {"one","two","three"}
setvar.remove("three")

#setvar.remove("three123") #error,删除原来没有的元素会出错
print(setvar)

#discard() 删除集合中指定的值 (不存在的不删除 推荐使用)
setvar = {"one","two","three"}
res = setvar.discard("three123")
print(setvar)

3.冰冻集合 (只能做交差并补)
#frozenset 可墙砖容器类型数据变为冰冻集合
#冰冻集合一旦创建,不能再进行任何修改,只能做交差并补

#(1) 声明空的冰冻集合
fz = frozenset()
print(fz,type(fz))
fz = frozenset([1,2,3,4])
print(fz,type(fz))
#fz2 =frozenset("2345")  #注意字符串类型
fz2 = frozenset((2,3,4,5))
print(fz2,type(fz2))

fz2 = fz | fz2  #并集
print(fz2,type(fz2))

#fz1.add("456") #error 不允许添加或者删除操作

 

:字典相关的函数

 ###字典的相关函数(增删改查)
# (1)
dictvar = {}
dictvar[‘top‘] ="剑圣"
dictvar[‘moddle‘] = "妲己"
dictvar[‘bottom‘] = "鲁班七号"
dictvar[‘jungle‘] = "刘备"
dictvar[‘support‘] = "刘邦"
dictvar[‘top‘] ="凯"
#如果键和前面的元素相同的情况下,将相同键的对应的值进行更新
print(dictvar)

#fromkeys() 使用一组键和默认值创建字典
listvar = ["a","b"]
dict = {}.fromkeys(listvar,None)
print(dict)

#不推荐使用fromkeys
‘‘‘
dict = {}.fromkeys(listvar,[])
dic[‘a‘].append(55)
print(dict(‘b‘))
‘‘‘

#(2) 删除
#pop() 通过键去删除键值对(若没有该键可设置默认值,预防报错)
dictvar = {‘top‘: ‘凯‘, ‘middle‘: ‘妲己‘, ‘bottom‘: ‘鲁班七号‘, ‘jungle‘: ‘刘备‘, ‘support‘: ‘刘邦‘}
dictvar.pop(‘middle‘)
print(dictvar)

#dictvar.pop("modegdiqg")
dictvar.pop("dgqidq","该键不存在")
print(dictvar)

#popitem()  删除最后一个键值对 3.6版本
dictvar = {‘top‘: ‘凯‘, ‘middle‘: ‘妲己‘, ‘bottom‘: ‘鲁班七号‘, ‘jungle‘: ‘刘备‘, ‘support‘: ‘刘邦‘}
dictvar.popitem()
print(dictvar)

#clear() 清空字典
dictvar.clear()
print(dictvar)

#(3)
#update() 批量更新(有该键就更新,没有该键就添加)
dict = { ‘jungle‘: ‘刘备‘, ‘support‘: ‘刘邦‘}
#dict.update{{‘top‘:"关羽",‘middle‘:"武则天"}}
#写法一 (推荐)
dict.update({‘top‘:"花木兰",‘middle‘:"武则天","support":"蔡文姬"})
print(dict)

#写法二:
dict.update(abc="23",bbb="3ew",ccc="4567")
print(dict)

#(4)
#get() 通过键获取值(若没有该键可设置默认值,预防报错)
dictvar = {‘top‘: ‘凯‘, ‘middle‘: ‘妲己‘, ‘bottom‘: ‘鲁班七号‘, ‘jungle‘: ‘刘备‘, ‘support‘: ‘刘邦‘}
#dictvar[‘top123‘] error
res = dictvar.get("top123")
#如果没有该键,默认返回None,如果指定默认值,则返回默认值
res = dictvar.get("top123","对不起,没有该键")
print(res)


#重点记忆:
#keys() 将字典的键组成新的迭代对象
dictvar = {‘top‘: ‘凯‘, ‘middle‘: ‘妲己‘, ‘bottom‘: ‘鲁班七号‘, ‘jungle‘: ‘刘备‘, ‘support‘: ‘刘邦‘}
for i in dictvar:
    print(i)

#能够遍历就证明具有可迭代性
res = dictvar.keys()
print(res)

for i in res:
    print(i)

#values() 将字典中的值组成新的可迭代对
res = dictvar.values()
print(res)
for i in res:
    print(i)

#items() 将字典的键值对凑成一个个元组,组成新的可迭代对象
res = dictvar.items()
print(res)
for i in res:
    print(i)

for a,b in res:
    print(a,b)

 

 

 

 

Python 基础之集合相关操作与函数和字典相关函数

标签:判断   middle   操作   集合   ==   版本   更新   strong   ===   

原文地址:https://www.cnblogs.com/hszstudypy/p/10849805.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!