数据类型的基本功能(三)

这个笔记主要是各个数据类型的方法(独有功能,公有功能),该部分内容多且繁琐,但常用的不多。因此,只需要记忆常用,实用的方法。

(一)int,bool,str

(二)list,tuple

(三)dict,set

结构: 数据类型.功能名称(......)

set类型

独有功能

一、添加元素,.add(x)

date = {12,345,'asd',(99,)}
print(date)
#{'asd', 345, 12, (99,)}#显示是有序的,但在计算机底层是无序的

date.add('520')
print(date)                                                        #把元素x添加到集合
#{'asd', '520', 12, 345, (99,)}#集合是可变的,可以增删

date.add(12)
print(date)
#{'asd', '520', 12, 345, (99,)}#集合中已存在的元素不会重复添加

二、删除元素,.discard(x)

date = {12,345,'asd',(99,)}
date.discard('asd')                                                #把元素x从集合删除
print(date)
#{345, 12, (99,)}

三、交集,.intersection()

#取集合的交集
seta = {123,456,789}
setb = {123,666,999}
setc = seta.intersection(setb)                              #求集合a与集合b的交集
print(setc)
#{123}
print(seta & setb)          #运算符求交集

四、并集,.union()

#取集合的并集
seta = {123,456,789}
setb = {123,666,999}
setd = seta.union(setb)                                    #求集合a与集合b的并集
print(setd)
#{789, 999, 456, 666, 123}
print(seta | setb)          #运算符求并集

五、差集,.difference()

#取集合的差集
seta = {123,456,789}
setb = {123,666,999}
sete = seta.difference(setb)                               #求集合a与集合b的差集
print(sete)
#{456, 789}
print(seta - setb)          #运算符求差集

公有功能

一、相减,( - )

#运算符求差集
seta = {123,456,789}
setb = {123,666,999}
print(seta - setb)
#{456, 789}
print(setb - seta)
#{666, 999}
'''
用集合a差集合b:集合a中有集合b中没有的元素
用集合b差集合a:集合b中有集合a中没有的元素
'''

二、交集,( & )

#运算符求交集
seta = {123,456,789}
setb = {123,666,999}
print(seta & setb)
#{123}

三、并集,( | )

#运算符求并集
seta = {123,456,789}
setb = {123,666,999}
print(seta | setb)
#{789, 999, 456, 666, 123}

四、计算长度

date = {12,345,'asd',(99,)}
print(len(date))                    #获取集合元素个数
#4

五、for循环集合

date = {12,345,'asd',(99,)}
for i in date:
    print(i)
#只能通过for+变量循环集合;不能通过for+range索引循环集合(因为集合是无序的)

dict类型

独有功能

一、根据键x获取值,.get(x)                键不存在返回None

       根据键x获取值, .get(x,y)            键不存在返回y

date = {'name':'helin',
        'age':21,
        'hobby':['soccer','swim'],
        ('1+1','=',2):True}
print(date)
'''
键只能是可哈希的不可变类型(str,int,bool,tuple),不能为可变类型(list,set,dict)
值可以任意取
'''
result = date.get('name')                   #返回键对应的值
print(result)
#helin
result = date.get('clothes')                #不存在的键,返回None
print(result)
#None

result = date.get('age','999')              #返回键对应的值
print(result)
#21
result = date.get('key','value')            #指定不存在的键,返回y
print(result)
#value
#案例,实现用户登录
user_set = {
    'hll':'123',
    'wby':'666'
}
user_name = input('请输入用户名:')
user_pwd = input('请输入密码:')

pwd = user_set.get(user_name)#根据用户输入的user_name作为键,在user_set中找到对应的值

#简单的逻辑放在前面
if not pwd:
    print('用户名不存在')
else:
    if pwd == user_pwd:
        print('登录成功')
    else:
        print('登录失败')

二、获取所有的键,.keys()

date = {'name':'helin',
        'age':21,
        'hobby':['soccer','swim'],
        ('1+1','=',2):True}
#print(date)

result = date.keys()
print(result)
#dict_keys(['name', 'age', 'hobby', ('1+1', '=', 2)])

三、获取所有的值,.values()

date = {'name':'helin',
        'age':21,
        'hobby':['soccer','swim'],
        ('1+1','=',2):True}
#print(date)

result = date.values()
print(result)
#dict_values(['helin', 21, ['soccer', 'swim'], True])

四、获取所有的键值,.items()

date = {'name':'helin',
        'age':21,
        'hobby':['soccer','swim'],
        ('1+1','=',2):True}
#print(date)

result = date.items()
print(result)
#dict_items([('name', 'helin'), ('age', 21), ('hobby', ['soccer', 'swim']), (('1+1', '=', 2), True)])
'''存放在一个元组包裹的列表中'''
#for循环

# 用一个变量接收元素
for item in result:
    print(item[0],item[1])
# 直接用两个变量接收元素的元素    
for key,value in result:
    print(key,value)

## 高仿列表dict_keys(), dict_values(), dict_items()

date = {'name':'helin',
        'age':21,
        'hobby':['soccer','swim'],
        ('1+1','=',2):True}
#print(date)
result = date.items()

#将高仿列表转化为列表
print(list(result))
# [('name', 'helin'), ('age', 21), ('hobby', ['soccer', 'swim']), (('1+1', '=', 2), True)]

#高仿列表for循环
for i in result:
    print(i)

# ('name', 'helin')
# ('age', 21)
# ('hobby', ['soccer', 'swim'])
# (('1+1', '=', 2), True)

#高仿列表运算符in判断
if ('name', 'helin') in result:
    print(('name', 'helin'))
else:
    print('不存在')
#('name', 'helin')

 五、设置值,.setdefault()

date = {'name':'helin',
        'age':21,
        'hobby':'soccer'}
date.setdefault('hobby','swim')
print(date)
#{'name': 'helin', 'age': 21, 'hobby': 'soccer'}
#键存在,不操作

date.setdefault('qq','123456789')
print(date)
#{'name': 'helin', 'age': 21, 'hobby': 'soccer', 'qq': '123456789'}
#键不存在,添加这个元素;

六、更新键值对,.undate()

date = {'name':'helin',
        'age':21,
        'hobby':'soccer'}
date.update({'name':'hll','hobby':'soccer','qq':'123456789'})#()内必须是一个字典,

print(date)
#{'name': 'hll', 'age': 21, 'hobby': 'soccer', 'qq': '123456789'}
#集合中键不存在,添加这个键值对
#集合中键存在,修改它的值

七、移除指定键值对,.pop(x)

#移除指定的键值对

date = {'name':'helin',
        'age':21,
        'hobby':'soccer'}
delete = date.pop('age')#根据键x删除原字典中的键值对,并可以根据键把对应值赋值给变量

print(date)
#{'name': 'helin', 'hobby': 'soccer'}
print(delete)
#21

八、按顺序移除,.popitem()

#python3.6+为有序操作(python3.6-字典为无序的)
date = {'name':'helin',
        'age':21,
        'hobby':'soccer'}
delete = date.popitem()#移除最后的键值对,并把移除的键值对存放以元组的形式

print(date)
#{'name': 'helin', 'age': 21}
print(delete)
#('hobby', 'soccer')

公有功能

 一、求并集

#python3.9才有的功能

dicta = {'aaa':111,'bbb':222,'ccc':333}
dictb = {'aaa':123,'ppp':666,'ccc':333}

dictc = dicta | dictb         #出现相同的键,后边覆盖前边的值
print(dictc)
#{'aaa':123,'bbb':222,'ccc':333,'ppp':666}

二、获取长度

date = {'name':'helin',
        'age':21,
        'hobby':'soccer'}
print(len(date))
#3

三、运算符包含

date = {'name':'helin',
        'age':21,
        'hobby':'soccer'}

judge = 'name' in date
print(judge)
#True
judge = 'name' in date.keys()
print(judge)
#True
judge = 'helin' in date.values()
print(judge)
#True
judge = ('name','helin') in date.items()
print(judge)
#True

四、索引取值,

date = {'name':'helin',
        'age':21,
        'hobby':'soccer'}
#有序不同于列表和元组,通过键作为索引取值
print(date['name'])
#helin
#print(date['qq'])#不存在的键会报错
#.get()不会报错

 五、增加,修改,删除

date = {'name':'helin',
        'age':21,
        'hobby':'soccer'}

#不存在的键索引,直接添加
date['qq'] = 123456789
print(date)
#{'name': 'helin', 'age': 21, 'hobby': 'soccer', 'qq': 123456789}

#存在的键索引,为修改键的值
date['age'] = 20
print(date)
#{'name': 'helin', 'age': 20, 'hobby': 'soccer', 'qq': 123456789}
#区别setdefault(),少

#删除键对应的键值对
del date['hobby']
print(date)
#{'name': 'helin', 'age': 20, 'qq': 123456789}
#不存在会报错
#区别pop(),可以赋值

六、for循环字典

date = {'name':'helin',
        'age':21,
        'hobby':'soccer'}
for i in date:
    print(i)
#默认就是字典键的循环
for key in date.keys():
    print(key)

for value in date.values():
    print(value)

for key,value in date.items():
    print(key,value)

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值