内容回顾
补充
列表
reverse,反转
v1 = [1,2,311,32,13]print(v1)v1.reverse()print(v1)
sort排序
# v1.sort(reverse=False) #从小到大(默认)v1.sort(reverse=True) #从大到小print(v1)
字典
key/values/items
get
info = {'k1':'v1','k2':'v2'}# print(info['k1111'])# v2 = info.get('k1111')# print(v2) #返回None# v3 = info.get('k1111',666)# print(v3) #返回666result = info.get('k111')print(result)if result == None: print('不存在')if result: print('存在')else: print('不存在') #None数据类型,改类型表示空(无任何功能,专门提供空值)
pop
# info = {'k1':'v1','k2':'v2'}# result = info.pop('k2')# print(info,result)## del info['k1']
update
nfo = {'k1':'v1','k2':'v2'}info['k3'] = 'v4'#不存在则添加,存在则替换info.update({'k3':'v3','k4':'v4'})print(info)
判断一个字符串是否有敏感字符?
str
v = "Python全栈21期"if "全栈" in v: print('含敏感字符')
List/tuple
v = ['alex','oldboy','高老四','利奇航']if '利奇航' in v: print('含敏感')
dict
v = {'k1':'v1','k2':'v2','k3':'v3'}#默认按照键判断,即:判断x是否是字典的键if 'x' in v: pass#请判断:k1是否在其中?if k1 in v: pass#请判断:v2是否在其中?#方式一:循环判断flag = '不存在'for v in v.values(): if v == v2: flag = '存在' print(flag)#方式二:#List(v.values()) #强制转换成列表['v1','v2','v3']for 'v2' in List(v.values): pass#请判断:k2:v2是否在其中?value = v.get('k2')if value == 'v2': print('存在')else: print('不存在')
练习题
#让用户输入任意字符串,然后判断此字符串是否包含指定的敏感字符char_List = ['利奇航','倘有光','炸掉四']content = input('请输入内容:') #我叫利奇航 / 我叫倘有光 / 我叫炸掉四success = Truefor v in char_List: if v in content: sucess = False breakif success: print(content)else: print('包含敏感字符') #示例:# 1.昨天上课最后一题# 2.判断'v2'是否在字典的value中 v = {'k1':'v1','k2':v2}# 3.敏感字
内容详细1.集合set无序不重复v = {1,2,3,4,5,6,99,100}#疑问:v = {}#1.空集合# v = {} #空字典# v1 = set()# print(type(v1))'''Noneint v1 = 123 v1 = int() -> 0bool v2 = True/False v2 = bool() -> Falsestr v3 = "" v3 = str()List v4 = [] v4 = List()tupe v5 = () v5 = tuple()dict v6 = {} v6 = dict()set v7 = set()'''
集合的独有功能
adddiscardupdateintersectionuniondifferencesymmetric_difference公共功能
len
v = {1,2,'lishaoqi'}print(len(v))
for循环
v = {1,2,'lishaoqi'}for item in v: print(item)
索引【无】
步长【无】
切片【无】
删除【无】
修改【无】
嵌套
#1.列表/字典/集合 ->不能放在集合中+不能作为字典的key()# info = {1,2,3,4,True,"国风",None,(1,2,3)}# print(info)#2. hash -> 哈希是怎么回事?#因为在内部会将值进行哈希算法并得到一个数值(对应内存地址),以后用于快速查找#3.特殊情况# info = {# 1:'alex',# True:'oldboy'# }# print(info) #{1: 'oldboy'}
2.内存相关示例一:
v1 = [11,22,33]v2 = [11,22,33]v1 = 666v2 = 666v1 = 'asdf'v2 = 'asdf'#按理 v1和v2应该是不同的内存地址,但python中具有小数据池缓存功能,特殊: 1.整型:-5 ~ 256 2.字符串:'alex','sefsdfe','weaff','wef_wef' ```"f*"3 重新开辟内存
示例二:再开辟一个内存空间
v1 = [11,22,33,44]v1 = [11,22,33]
示例三:指向同一个内存地址
v1 = [11,22,33]v2 = v1#练习1(内部修改)v1 = [11,22,33]v2 = v1v1.append(666) #内部修改print(v2) #[11, 22, 33, 666]#练习2(赋值)v1 = [11,22,33]v2 = v1v1 = [1,2,3,4] #赋值print(v2) #[11, 22, 33]#练习3:(重新赋值)v1 = 'alex'v2 = v1v1 = 'oldboy'print(v2) #alex
示例四
v = [1,2,3]values = [11,22,v]#练习1:v.append(9)print(values) #[11,22,[1,2,3,9]]#练习2:values[2].append(999)print(v) #[1,2,3,999]#练习3:v = 999print(values) #[11, 22, [1, 2, 3]]#练习4:values[2] = 666print(v) #[1,2,3]
示例五
v1 = [1,2]v2 = [2,3]v3 = [11,22,v1,v2,v1]
查看内存地址
# v1 = [1,2,3]# v2 = [1,2,3]# v3 = v2# print(ID(v1),ID(v2),ID(v3))'''v1 = [1,2,3]v2 = v1print(ID(v1),ID(v2))v1 = 999print(ID(v1),ID(v2))'''
问题: == 和 is 有什么区别?
==用于比较值是否相等
is用于比较内存地址是否相等
总结列表reversesort字典getupdate集合adddiscardupdateintersectionuniondifferencesymmetric_difference特殊嵌套:集合/字典的key空:None空集合IDtype嵌套的应用赋值修改内部元素:列表/字典/集合 总结以上是内存溢出为你收集整理的Python学习笔记:day06 数据类型(集合)& 内存相关全部内容,希望文章能够帮你解决Python学习笔记:day06 数据类型(集合)& 内存相关所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)