Python set&dict

文章目录

集合

####集合set
###定义 s = {多个对象,} s = set(可迭代对象)
s0 = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
s1 = set("abracadabra")
s2 = set("alacazam")
print("s0:{}".format(s0))
print("s1:{}".format(s1))
print("s2:{}".format(s2))


###方法
##差集 -
print("s1 - s2 :{}".format(s1 - s2))

##并集 |
print("s1 | s2 :{}".format(s1 | s2))

##交集 &
print("s1 & s2 :{}".format(s1 & s2))

##对称差集 ^ 对称差集 = 并集 - 差集
print("s1 ^ s2 :{}".format(s1 ^ s2))

##判断子集 <=
print("s1 <= s2:{}".format(s1 <= s2))

##判断真子集 <
print("s1 < s2:{}".format(s1 < s2))

##判断有无交集
# set.isdisjoint(set1) 判断有无交集,返回bool型
print("s1.isdisjoint(s2):{}".format(s1.isdisjoint(s2)))

##增
# set.updata(*others) others可以为一个或者多个值,但是必须是可迭代对象
s3 = {"a", "b", "c", "d"}
print("s3:{}".format(s3))
s3.update("xyz")
print("s3.update(\"xyz\"):{}".format(s3))

# set.add(elem) 增加一个值
s3 = {"a", "b", "c", "d"}
print("s3:{}".format(s3))
s3.add(7)
print("s3.add(7):{}".format(s3))

##删
# set.remove(elem) 移除一个元素,若元素不存在,抛keyerror异常
s3 = {"a", "b", "c", "d"}
print("s3:{}".format(s3))
s3.remove("a")
print("s3.remove(\"a\"):{}".format(s3))

# set.discard(elem) 移除一个元素,若元素不存在,什么都不做
s3 = {"a", "b", "c", "d"}
print("s3:{}".format(s3))
s3.discard(7)
print("s3.discard(7):{}".format(s3))

# set.pop() 随机弹出一个值,若对空集使用此方法,抛keyerror异常
s3 = {"a", "b", "c", "d"}
print("s3:{}".format(s3))
c1 = s3.pop()
print("s3.pop():{},c1:{}".format(s3, c1))


###补充
# 可变,无序,不重复元素的合集,自动去重,集合中不需要所有元素的类型一致
# set的元素必须是可哈希类型,即list不可以
-----------------------------------------
s0:{'pear', 'orange', 'apple', 'banana'}
s1:{'b', 'a', 'r', 'c', 'd'}
s2:{'a', 'c', 'z', 'm', 'l'}
s1 - s2 :{'r', 'b', 'd'}
s1 | s2 :{'b', 'a', 'l', 'r', 'c', 'z', 'm', 'd'}
s1 & s2 :{'c', 'a'}
s1 ^ s2 :{'z', 'm', 'b', 'd', 'r', 'l'}
s1 <= s2:False
s1 < s2:False
s1.isdisjoint(s2):False
s3:{'c', 'b', 'a', 'd'}
s3.update("xyz"):{'b', 'a', 'x', 'y', 'c', 'z', 'd'}
s3:{'c', 'b', 'a', 'd'}
s3.add(7):{'b', 'a', 7, 'c', 'd'}
s3:{'c', 'b', 'a', 'd'}
s3.remove("a"):{'c', 'b', 'd'}
s3:{'c', 'b', 'a', 'd'}
s3.discard(7):{'c', 'b', 'a', 'd'}
s3:{'c', 'b', 'a', 'd'}
s3.pop():{'b', 'a', 'd'},c1:c

字典

####字典
###定义 dic = {key1:value1,key2:value2} dic = dict(可迭代键值对),其中注意key的写法
dic1 = {"name": "agsol", "age": 16, "gender": "male"}
dic2 = dict(name="james", age=39)
print("dic1:{}".format(dic1))
print("dic2:{}".format(dic2))


###方法
##增
# d[key]=value 若key存在,更新value的值,若key不存在,添加键值对
dic2["gender"] = "male"
print("dic2[\"gender\"]=\"male\":{}".format(dic2))

# d.update([otherdict]) 将otherdict中的键值对添加到d中,key重复的覆盖value值,key不存在添加键值对
dic3 = {"english": 100, "math": 150}
dic1.update(dic3)
print("dic1.update(dic3):{}".format(dic1))

##删
# d.pop(key[,default]) key存在,返回对应value,并移除键值对;key不存在,返回default
english = dic1.pop("english")
print("dic1.pop(\"english\"):{},english:{}".format(dic1, english))

# d.popitem() 移除并返回任意一个键值对,对空字典使用,会抛KeyError异常
rkey, rvalue = dic1.popitem()
print("dic1.popitem():{},rkey:{},rvalue:{}".format(dic1, rkey, rvalue))

##改


##查
# dic = d[key] 按key访问,key不存在抛 KeyError异常
print("dic1[\"name\"]:{}".format(dic1["name"]))

# dic = d.get(key[,default]) 按key访问,key不存在返回缺省值,默认缺省值None
print("dic1.get(\"name\",\"Tom\"):{}".format(dic1.get("name", "Tom")))
print("dic1.get(\"xyz\",\"Tom\"):{}".format(dic1.get("xyz", "Tom")))

# dic = d.setdefault(key[,default]) 按key访问,key不存在添加键值对,并返回value值为default,default默认None,若key存在,不改变对应value
print("dic2.setdefault(\"gender\",\"male\"):{}".format(dic2.setdefault("gender", "male")))
print("dic2:{}".format(dic2))

##遍历
# 遍历key
print("~~~key~~~")
for i in dic1:
    print(i, dic1[i])

# 遍历value
print("~~~value~~~")
for i in dic1.values():
    print(i)

# 遍历item
print("~~~items~~~")
for k, v in dic1.items():
    print(k, v)


###补充
# 可变,无序,不可哈希
# 有序字典, 需要使用collections包
import collections

dic = collections.OrderedDict()
dic["a"] = "A"
dic["b"] = "B"
dic["c"] = "C"
dic["d"] = "D"
print("~~~collections.OrderedDict()~~~")
for k, v in dic.items():
    print(k, v)
-----------------------------------------
dic1:{'name': 'agsol', 'age': 16, 'gender': 'male'}
dic2:{'name': 'james', 'age': 39}
dic2["gender"]="male":{'name': 'james', 'age': 39, 'gender': 'male'}
dic1.update(dic3):{'name': 'agsol', 'age': 16, 'gender': 'male', 'english': 100, 'math': 150}
dic1.pop("english"):{'name': 'agsol', 'age': 16, 'gender': 'male', 'math': 150},english:100
dic1.popitem():{'name': 'agsol', 'age': 16, 'gender': 'male'},rkey:math,rvalue:150
dic1["name"]:agsol
dic1.get("name","Tom"):agsol
dic1.get("xyz","Tom"):Tom
dic2.setdefault("gender","male"):male
dic2:{'name': 'james', 'age': 39, 'gender': 'male'}
~~~key~~~
name agsol
age 16
gender male
~~~value~~~
agsol
16
male
~~~items~~~
name agsol
age 16
gender male
~~~collections.OrderedDict()~~~
a A
b B
c C
d D
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值