元组常用方法
元组,元素不可被修改,不能被增加或者删除
tu = (11,22,33,44)
1 1.count:统计字符数目
2 tu.count(22)# 获取指定元素在元组中出现的次数
3 2.index:获取索引
4 tu.index(22)
list,tuple,str相互转换
1 s = "asdfasdf0"
2 li = ["asdf","asdfasdf"]
3 tu = ("asdf","asdf")
4 v = tuple(s)
5 print(v)
6
7 v = tuple(li)
8 print(v)
9
10 v = list(tu)
11 print(v)
12
13 v = "_".join(tu)
14 print(v)
字典常用方法
1 dic = { "k1": 'v1',
2 "k2": 'v2'
3 }
4 # 1 fromkeys:根据序列,创建字典,并指定统一的值
5 v = dict.fromkeys(["k1",123,"999"],123)
6 print(v)
7
8 # 2 get:根据Key获取值,key不存在时,可以指定默认值(None)
9 v = dic['k11111']
10 print(v)
11 v = dic.get('k1',111111)
12 print(v)
13
14 # 3 pop,popitem:删除并获取值
15 dic = {
16 "k1": 'v1',
17 "k2": 'v2'
18 }
19 v = dic.pop('k1',90)
20 print(dic,v)
21 k,v = dic.popitem()
22 print(dic,k,v)
23 """
24 # 4 setdefault:设置值,
25 已存在,不设置,获取当前key对应的值
26 不存在,设置,获取当前key对应的值
27 """
dic = {
28 "k1": 'v1',
29 "k2": 'v2'
30 }
31 v = dic.setdefault('k1111','123')
32 print(dic,v)
33
34 # 5 update更新
35 dic = {
36 "k1": 'v1',
37 "k2": 'v2'
38 }
39 dic.update({'k1': '111111','k3': 123})
40 print(dic)
41 dic.update(k1=123,k3=345,k5="asdf")
42 print(dic)