Python基础(六):散列类型(集合和字典)

集合

python中集合用 { } 表示。空集用 set() 来表示。

集合有两种特性:1)无序性:集合中的元素没有顺序。

                            2)唯一性:集合中的元素是唯一的,即使在建立的时候存在相同的元素,输出的元素也是唯一的。

集合中不能存在任何的可变类型,集合本身是一种可变的数据类型。也就是说集合不能嵌套集合。

集合的运算:集合主要有交并差三种运算,交集:s1&s2

                                                                  并集:s1 | s2

                                                                  差集:s1 - s2

s1={2,3,4,5,6,7,'hello','python'}
s2={4,5,6,7,8,9,'hello','world'}
print('交集:',s1&s2)
print('并集:',s1|s2)
print('差集:',s1-s2)

结果

交集: {'hello', 4, 5, 6, 7}
并集: {'python', 2, 3, 4, 5, 6, 7, 'world', 8, 9, 'hello'}
差集: {'python', 2, 3}

 

集合元素的增加:add()和update( iterable ),其中update中可以添加的是可迭代对象,列表,字符串等,它会把可迭代对象依次一个个的添加到集合中。看例子。

s1={2,3,4,5,6,7,'hello','python'}
s1.add('hi')
print(s1)
s1.update([77,88,99])
print(s1)
s1.update('world')
print(s1)

结果

{2, 3, 4, 5, 6, 'python', 7, 'hi', 'hello'}
{2, 3, 4, 5, 6, 'python', 7, 'hi', 99, 'hello', 77, 88}
{2, 3, 4, 5, 6, 'python', 7, 'hi', 99, 'l', 'hello', 77, 'd', 'o', 88, 'r', 'w'}

集合元素的删除:

pop()会随机删除集合中的一个元素,并返回删除的值。remove(object)会删除指定的元素。clear()清空所有元素。

s1={3,4,5,6,7,'hello','python'}
s1.pop()
print("pop():",s1)

结果
pop(): {4, 5, 6, 7, 3, 'hello'}
s1={3,4,5,6,7,'hello','python'}
s1.remove('hello')
print('remove():',s1)

结果
remove(): {3, 4, 5, 6, 7, 'python'}

集合元素的查找

s1={3,4,5,6,7,'hello','python'}
s2={4,5,6,7,'hello'}
print(s1.isdisjoint(s2))  #是否无交集,有返回False,无返回True
print(s1.issubset(s2))  #s1是否包含于s2,是返回True
print(s1.issuperset(s2)) #s1是否包含s2,是返回True

结果
False
False
True

字典

python中字典也是一种无序的集合,字典中的元素是通过键来存取的。

字典的特性:1)无序性;

                     2)key:value形式存在;

                     3)key值唯一且不可变,如果重复后面的会覆盖前面的。

空字典定义:di={}或di=dict()

di1={'username':'austinjoe','password':'hahaha','age':22}
print(di1.get('username'))  #若查找的键不存在返回None
print(di1['username'])      #若查找的键不存在,报错

#结果:
austinjoe
austinjoe
di1={'username':'austinjoe','password':'hahaha','age':22}
print(di1.keys())
print(di1.values())
print(di1.items())
#注意以上三种方法返回的值的类型是python内置的类型,若要使用首先应该类型转换
print("转换成列表:",list(di1.keys()))

#结果
dict_keys(['username', 'age', 'password'])
dict_values(['austinjoe', 22, 'hahaha'])
dict_items([('username', 'austinjoe'), ('age', 22), ('password', 'hahaha')])
转换成列表: ['username', 'age', 'password']

删除

di1={'username':'austinjoe','password':'hahaha','age':22}
print(di1.pop('password'))#删除指定键的值
print(di1)
di1={'username':'austinjoe','password':'hahaha','age':22}
print(di1.popitem())#随机删除并返回删除内容
print(di1)

结果:
hahaha
{'username': 'austinjoe', 'age': 22}
('username', 'austinjoe')
{'age': 22, 'password': 'hahaha'}

设置默认值

di1={'username':'austinjoe','password':'hahaha','age':22}
di1.setdefault('name','joe')#添加默认值
print(di1)
di1.setdefault('password','idontknow')#若key值不存在,则value值无效
print(di1)

结果:
{'age': 22, 'username': 'austinjoe', 'password': 'hahaha', 'name': 'joe'}
{'age': 22, 'username': 'austinjoe', 'password': 'hahaha', 'name': 'joe'}

合并

di1={'username':'austinjoe','password':'hahaha','age':22}
di1.update({123:45})
print(di1)
di1.update({'username':789}) #若key值存在,则覆盖
print(di1)

结果:
{'password': 'hahaha', 'age': 22, 123: 45, 'username': 'austinjoe'}
{'password': 'hahaha', 'age': 22, 123: 45, 'username': 789}

fromkeys()构造技术

print(dict.fromkeys(['hello','python'],'good'))
print(dict.fromkeys(['hello','python']))

结果:
{'python': 'good', 'hello': 'good'}
{'python': None, 'hello': None}

 

转载于:https://www.cnblogs.com/austinjoe/p/9394505.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值