python05元组字典

1、元组操作

#将列表转成元组  优势 元组不可变
list = [1,2,3]
t15 = tuple(list)
print(t15)

#截取 .split(str= "*",num) 截取字符串遇到*号截取,num四几次
str1 = "wan****quan*zui****shuai"
str2 = str1.split("*",8)
print(str2)

#按行切割 splitlines([keepends]) 用于网络截取的文件比较大,所以一行的处理
str3 = '''wan****
quan*zui**
**shuai
'''
print(str3.splitlines())

#组合字符串 放在字符串中间
list1 = ["wan","quan","zui","shuai"]
str5 = "*".join(list1)
print(str5)

#replace(oldstr,newstr,count)count 指定前count个
# 字符串不能变 要替换字符串中,那么最好从新给定 替换
str6 = "wan quan zui shuai"
str7 = str6.replace("shuai","nice",1)
print(str7)
#一一对应 maketrans("xy","12")吧x对应1 y对应2  #好像有问题
str8 = "wan quan zui shuai"
str9 = str8.maketrans("wq","12")
print(str9)

#对照表  ????????
str10 = "wan quan"
str11 = "zui shuai"
str12 = str11.translate(str10)
print(str12)

#高级
#判断是不是以某个参数 开头    startswith("",start,end) 没有范围就是全部字符串
str13 = "wan qua zui shuai"
print(str13.startswith(("wan",0,5)))
#判断是不是以某个参数 结束 endswith

#编码
#encode(encoding="utf-8",errors="strict")
str14 = "wan quan zui shuai"
str15 = str14.encode("utf-8")
print(str15)   #打印值为b' wan quan zui shuai' 相当于二进制编码
#解码
str16 = str15.decode("utf-8","ignore") #ignore如果编解码前后不一致那么久不处理
print(str16)


#isalpha()
#如果字符串中至少有一个字符且所有的字符都是字母 返回True  空格也算其他字符
str17 = "wan quan zui shuai"
print(str17.isalpha())
#isalnum()
#如果字符串中至少有一个字符且剩下的必须为数字和字母返回True
str18 = "123s456"
print(str18.isalnum())
#isupper()
#字符串中至少带有一个英文字符且所有的字符都是大写的英文字母,返回True
print("ABC".isupper())
print("1C".isupper())
print("1".isupper())
print("ABC###".isupper())
print("ok")
#islower()
#字符串中至少带有一个英文字符且所有的字符都是小写的英文字母,返回True

#istitle()
#如果字符串是标题划的返回True  Wan Quan
print("Wan Quan".istitle())
#isdigit() = isnumeric字符串只包含数字返回True
print("123".isdigit())
#isdecimal()字符串只包含十进制的字符True 其他与上面一致

#isspace只包含空格 \t 返回True
print("".isspace())
print("  ".isspace())
print("\t".isspace())


2、字典
#使用键值 key value 建立表格
#key的特征:
'''
1、字典中的key必须唯一
2、key必须是不可变对象
3、字符串、整数等都是不可变的,可以作为key
4、list是可变的,不能做为key
5、字点存储是无序的

保存学生姓名成绩
学生为key
'''



dictl = {"tom":60,"ben":70}
#元素的访问
#获取:根据key获取值
print(dictl["tom"])
#如果没有想要的数据那我们需要进行判断


#增加 dictl["wan"] = 100
dictl["wan"] = 100
#删除 dictl.pop("tom")
#修改 key  dictl["tom"] = 100
#查
print(dictl.get("wan"))  #一般用这个判断 防止出错
ret = dictl.get("tom")
if ret == None:
    print("no")
else:
    print("yes")

#遍历
for key in dictl:
    print(key,dictl[key])

print(dictl.values())   #dict_values([60, 70, 100])

for value in dictl.values(): #遍历所有值
    print(value)
#打印数组
print(dictl.items()) #dict_items([('tom', 60), ('ben', 70), ('wan', 100)])
# 为了好看一般把他们分别显示出来
for k,v in dictl.items():
    print(k,v)

#按序提取 单数存储是无序的
for i,v2 in enumerate(dictl):
    print(i,v2)
#字典和list比较
#字典
#1、查找和插入速度极快,不会随着key-value的增加而变慢
#2、需要占用大的内存,内存浪费多
#list
#1、查找和插入的速度随着数据量的增多而减慢
#2、占用空间小,浪费内存小



 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值