一、元组(tuple)
元组:元组的排列是有顺序的,可以进行切片,元组的数据不可以进行修改
建议:元组在写的时候在最后加逗号(,),以区分参数数据
例:
tu = (111,"alex",(11,22),[(33,44)],True,22,33,44,)
元组相当于对列表的二次加工
元组具有2条功能
count和index
1、count
对元组的指定数据进行查询,返回数量
tu = (11,22,33,11,22,33,11,22,) v = tu.count(11) print(v)
2、index
查询元组的位置
tu = (111,"alex",(11,22),[(33,44)],True,22,33,44,) x = tu.index(111) v = tu[x] print(v) print(x)
3、对元组进行切片
tu = (111,"alex",(11,22),[(33,44)],True,22,33,44,) 切片 v = tu[0:2] print(v)
4、元组、字符串、列表的转换
s = "hfaflsdlfk0" li = ["asdad",1231] tu = ("123","123","kfadsj",) # 字符串转换元组 v = tuple(s) print(v) # 列表转换元组 v1 = tuple(li) print(v1) # 元组转换字符串 v2 = "".join(tu) print(v2)
二、字典(dict)
字典中有“键值对”,前面的值为key,后面的值为value
字典的value:可以是任意值
字典的key:可以为数字、字符串、布尔值
注:元组、列表不能作为字典的key,布尔值的Ture=1,False=0
字典是无序的,所以不能进行切片
info = {"k1":18, "k2":True, "k3":[11, 22, [], (), 33, {"kk1":"vv1", "kk2":"vv2", "kk3":(11,22) } ], "k4":(11,22,33,44) }
字典的功能
clear、copy、fromkeys、get、items、keys、pop、popitem、setdefault、update、balues
1、clear清除
v = {"k1":18,"k2":19} x = v.clear() print(x)
2、copy复制
v = {"k1":18,"k2":19} x = v.copy() print(x)
3、fromkeys使用列表等方式创建新key及value
v = dict.fromkeys(["k1",123,"999"],123) print(v) dict
4、get在查找key对应的值时,如果查找值没有,可以避免报错,直接报none
dic = {"k1":"v1"} v = dic.get("k1") v1 = dic.get("v1") print(v,v1)
5、pop可以删除对应值,也可以同时得到删除的值(pop获取的值为value)
dic = {"k1":"v1","k2":"v2"} v = dic.pop("k1") print(dic,v)
6、setdefault 不设置值,获取当前key对应的值,如果查询的key不存在,则在字典中增加值
dic = {"k1":"v1","k2":"v2"} v = dic.setdefault("k1","123") v1 = dic.setdefault("k1111","123") print(dic,v,v1)
7、update 对字典进行更新升级
dic = {"k1":"v1","k2":"v2"}
dic.update({"k1":"1111","k3":123})
print(dic)
#第二种写法
dic.update(k1=123,k3=345,k5="aafa")
print(dic)
三、整理
1、数字
int()
+ - * /
2、字符串
str()
replace、find、join、strip、startswith、split、upper、lower、format
3、列表
list()
append、extend、insert
索引、切片、循环
4、元组
索引、切片、循环
5、字典
get、update、keys、values、items
6、布尔值
bool()
0、1
假:None,[](){},0
标签:22,python,dic,v1,k1,print,元组,第七天 From: https://www.cnblogs.com/king-why/p/17632829.html