python 集合和字典笔记


set集合

  • 类似数学集合概念
  • 元素不可重复
  • 元素无序排列
  • 元素种类可不一

集合创建

  • 用大括号{ }创建
#集合创建
a = set()
print(type(a))#集合元素不可重复
a = {1,2,2,3,3,3,4,100,99}
print(a)   #输出的为不重复元素,且也是无序的元素#成员检测
a = {1,2,2,3,'music'}
if 'music' in a:
    print('你猜对了')#元素遍历
for i in a:
    print(i)
    
#类型转化 tuple change to set
a = (12,'music','program','code',1,2,2)
b = set(a)
print(b)

#结果:
<class 'set'>
{1, 2, 3, 100, 4, 99}
你猜对了
1
2
3
music
{'program', 1, 2, 'code', 12, 'music'}

集合的生成式

#集合的生成式
a = {1,2,3,4,5,6,7,8,9,10}
b = {i for i in a if i % 2==0}   #{ } 内部使用for循环和if语句进行新的集合的生成
print(b)

#结果:
{2, 4, 6, 8, 10}

集合中的内置函数

  • add 添加元素
  • clear 清空集合
  • remove/discard 删除指定元素
#add添加元素
a = {10,20,30,40,50}
a.add(60)
print(a)  #注意不可直接打印print(a.add(60))#clea清空元素
a.clear()
print(a)
​

```python
#remove删除指定元素
a = {1,2,3,4,5}
a.remove(1)
print(a)    #注意:discard也为删除指定元素(与remove区别在于remove删除原本不存在的元素时会报错,然而discard不会)
​
​#结果:
{40, 10, 50, 20, 60, 30}
set()
{2, 3, 4, 5}

## 集合pop操作(任意弹出集合中的内容)

```python
#pop操作
a = {10,22,33,54,15,26}
a.pop()
print(a)  #任意弹出一个元素,也可观察到集合中元素无序

#结果:
{10, 15, 54, 22, 26}

集合的数学操作

  • 并集(union) 所有部分
  • 交集(intersction) 共同拥有的部分
  • 差集(difference) 两个集合相减
#union并集
a = {1,2,3}
b = {2,3,6}
print(a.union(b))#intersection交集
print(a.intersection(b))#difference差集
a = {1,2,3}
b = {2,3,6}
print(a.difference(b))

#结果:
{1, 2, 3, 6}
{2, 3}
{1}

字典 - dict

  • 字典是一种组合数据,没有顺序的组合数据,以键值对(key-value)的方式实现
#创建字典
#方法1
d = {}
print(type(d))
#方法2
d = dict()
print(type(d))
#方法3
d = {'name':'xiaobai','age':18,'height':180}  #常用方法
print(type(d))
print(d)
#方法4利用字典
d = dict({'name':'xiaobai','age':18,'height':170})
print(d)
#方法5利用关键字参数
d = dict(name='xiaobai',age=18,height=160)
print(d)
#利用利用元组
d = dict([('name','xiaobai'),('age',18),('height',150)])
print(d)


#结果:​
<class 'dict'>
<class 'dict'>
<class 'dict'>
{'name': 'xiaobai', 'age': 18, 'height': 180}
{'name': 'xiaobai', 'age': 18, 'height': 170}
{'name': 'xiaobai', 'age': 18, 'height': 160}
{'name': 'xiaobai', 'age': 18, 'height': 150}

字典特征

  • 是序列的类型,但是无序序列,所以没有分片和索引
  • 均有键值对构成,即(kv)对
    • key(键):必须是可哈希的值:如int,float,tuple,string.不可以是:list,set,dict
    • value(值):任何值

字典的常见操作

  • 访问: 字典名称.[key]
  • 删除: del 字典名称.[key]
  • 成员检验 : in 和 not in
#字典的访问
d = {'one':1,'tow':2,'three':3}
print(d['one'])#字典的删除
del d['one']
print(d)#结果:​
1
{'tow': 2, 'three': 3}
#成员检测
#检测的是键而不是值
d = {'one':1,'tow':2,'three':3}if 'one' in d:
    print('哈哈')
if 1 in d:       #检测的是键而不是值
    print('哈哈')#结果:​
哈哈
#字典的遍历
d = {'one':5,'tow':6,'three':7}
for a in d:
    print(a, d[a])
print('*'*10)    
#访问所有键:
for i in d.keys():
    print(i)
print('*'*10)    
#访问所有值:
for i in d.values():
    print(i)
    
#结果:​
one 5
tow 6
three 7
**********
one
tow
three
**********
5
6
7
#特殊操作items()
#返回可遍历的(键, 值) ,以元组形式输出
d = {'one':5,'tow':6,'three':7}for a,b in d.items():
    print(a,b)
    
#结果:​
one 5
tow 6
three 7

字典生成式

  • 常规操作 {for…}
  • 加限制条件操作 {for… if…}
#字典生成式(常规操作)
d = {'one':1,'two':2,'three':3}
​
dd = {a:b for a,b in d.items()}
print(dd)
print('*'*10)#字典生成式(加限制条件操作)
print({a:b for a,b in d.items() if b % 2 ==0})

#结果:​
{'one': 1, 'two': 2, 'three': 3}
{'one': 1, 'two': 2, 'three': 3}
**********
{'two': 2}

字典相关函数

  • len() 返回字典中内容总个数
  • max()/min() 返回键中最大值或最小值
    • max(dict,key=dict.get) 返回字典中最大的值所对应的键
  • dict() 返回字典
  • str() 以字符串的形式返回字典内容
  • clear() 清空字典
  • itmes() 以元组的形式返回字典的键值
  • keys() 返回字典中所有键所组成的结构
  • values() 返回字典所有值所构成的结构
  • get() 根据键的输入,返回对应的值.若字典中没有该键,则默认返回None.
    - 也可设置默认返回值,在第二个参数上填入.但不代表字典中存在该键值对
  • dict.fromkeys() 使指定序列作为键,使一个值作为字典中所有键的值

字典相关函数的案例

d = {'b':4,'c':5,'d':12,'e':10}
     
#len()
print(len(d))
#max()
print(max(d))
#max(dict,key = dic.get)
print(max(d,key = d.get))
#str()
print(str(d))
#keys()
print(d.keys())
#clean()
print(d.clear())
print(d)
4
e
d
{'b': 4, 'c': 5, 'd': 12, 'e': 10}
dict_keys(['b', 'c', 'd', 'e'])
None
{}
#get()用法
d = {'one':1,'two':2,'three':3}
print(d.get('one'))
print(d.get('four',4))
print(d)


#结果:​
1
4
{'one': 1, 'two': 2, 'three': 3}
#fromkeys()案例
d = dict.fromkeys(range(5),'happy')
print(d)
c = dict.fromkeys(['one','two'],'TO')
print(c)
{0: 'happy', 1: 'happy', 2: 'happy', 3: 'happy', 4: 'happy'}
{'one': 'TO', 'two': 'TO'}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值