元祖(tuple)
- 对象用 tu = () ,如 tu = (11,22,33,44)
- 元祖与列表不同,元祖的一级元素不可修改、增加、删除(元祖中嵌套的列表可修改)
tu = (11,"umbrella",(25,5),[(45,89),11],True,22,33,)
tu[3][0] = 123
print(tu)
- 写元祖的时候,推荐最后加一个“,”,可以用于区分,如 如 tu = (11,22,33,44,)
- 可以通过索引取值(元祖有序),如 v = tu[0]
- 可以切片,如 v = tu[0:2]
- 可以进行for循环(可迭代对象:列表、字符串、元祖可相互转化;join、extend等可用),如 for item in tu: print(item)
li = [125,1,8,9,125,75] v = "umbrella" v1 = tuple(v) v2 = tuple(li) print(v1,v2)
元祖类中的功能
- count() 获取指定元素在元素中出现的次数
tu = (11,"umbrella",(25,5),[(45,89),11],True,22,33,11,) v = tu.count(11) print(v)
- index() 找某个元素在元祖中的位置,默认从左往右搜索,可设置起始位置和结束位置的参数
tu = (11,"umbrella",(25,5),[(45,89),11],True,22,33,11,) v = tu.index(22) print(v)
- in 找某个key或者value是否在字典中
dict = {"k1":"v1","k2":"v2"} v1 = "k1" in dict v2 = "k1" in dict.keys() v3 = "v1" in dict.values() print(v1,v2,v3)
字典(dict)
- 用大括号 “{}” ,由键值对组成,键值对内用 “:”,键值对之间用 “,” 例如 info = {"a1":"b1","a2":"b2"}
- 字典键值对中的value可以是任意值,数字、字符串、列表、布尔值、元祖都可
- 字典键值对中的key不可以是列表、字典,但key不可以重复,重复只保留一个其他的不显示(PS True代表1,False代表0)
- 字典是无序的,多次print(info)会发现每次结果不同
- 字典中可以利用索引得到或删除某个值,但因无序需根据自己定义的key找
info = {"a1":"b1","a2":"b2",857:True,"v":[4,8,6],666:(44,66),True:"123",}
print(info[857])
print(info["a1"])
print(info["v"][0])
del info["a1"]
print(info)
- 可进行for循环,默认输出key,可以选择.values输出value;选择.items同时输出key和value
info = {"a1":"b1","a2":"b2",857:True,"v":[4,8,6],666:(44,66),} for v1 in info: print(v1) for v2 in info.keys(): print(v2) for v3 in info.values(): print(v3)
for v4,v5 in info.items():
print(v4,v5)
dict 类中的方法
- clear() 清除字典
info = {"a1":"b1","a2":"b2",857:True,"v":[4,8,6],666:(44,66),} info.clear() print(info)
- copy() 拷贝字典
info = {"a1":"b1","a2":"b2",857:True,"v":[4,8,6],666:(44,66),} info.copy() print(info)
- fromkeys() 根据序列创建字典,并指定统一的值
info = {"a1":"b1","a2":"b2",857:True,"v":[4,8,6],666:(44,66),} v = dict.fromkeys(["223","sdf",666,"999"],1) print(v)
- get() 根据key取字典中的值,不同于找不到直接报错的索引,get可以指定没找到时候的输出值
dict = {"a":"b",857:True,"v":[4,8,6],666:(44,66),} v1 = dict.get("a") v2 = dict.get(456) v3 = dict.get(456,"错误") print(v1,v2,v3)
- items() keys() values() 选择要输出的内容,全部或key或value
info = {"a1":"b1","a2":"b2",857:True,"v":[4,8,6],666:(44,66),} for v1 in info: print(v1) for v2 in info.keys(): print(v2) for v3 in info.values(): print(v3) for v4,v5 in info.items(): print(v4,v5)
- pop() 删除key对应的值并输出删除值,也可通过添加参数将输出值改成指定内容
dict = {"a":"b",857:True,"v":[4,8,6],666:(44,66),} v1 = dict.pop(857) print(dict,v1) v2 = dict.pop(857,"换了") print(dict,v2)
- popitem() 随机删除
dict = {"a":"b",857:True,"v":[4,8,6],666:(44,66),} v1,v2 = dict.popitem() print(dict) print(v1,v2)
- setdefault() 设置值,如果已经存在则获取当前key对应的值,如果不存在则设置成对应值
dict = {"a":"b",857:True,"v":[4,8,6],666:(44,66),} v1 = dict.setdefault("a",666) v2 = dict.setdefault("umbrella",666) print(dict,v1) print(dict,v2)
- update() 更新
dict = {"k1":"v1","k2":"v2"} dict.update(k1=1,k2=2,k3=666) print(dict)
布尔值(bool)
- bool(...)
- True代表1
- Flase代表0
- 几种False的情况:None “” () [] {} 0
v1 = bool(None) v2 = bool("") v3 = bool(()) v4 = bool([]) v5 = bool({}) v6 = bool(0) print(v1,v2,v3,v4,v5,v6)