## 元组 tuple
1 常用的操作方法+内置方法
#优先掌握的操作:
#1 按照索引取值(正方向):只能取:
t1 = (11, 22, 33, ‘sea‘, [1, 66, 77, 100]) print(t1[4][1])
t1 = (11, 22, 33, ‘sea‘, [1, 66, 77, 100]) # 取值print(t1[2]) #切片 (顾头不顾尾,步长) print(t1[0:3])print(t1[0:4],3) #长度 len() print(len(t1)) # 成员运算 in , not in print(‘xxx‘ in t1) print(‘xxx‘ not in t1) ## 循环 for i in t1: print(i) #### 需要掌握的操作: t = (‘a‘, 2, 3, 4, 5, 6, 1, 11)print(t.index(‘a‘))print(t.count(1))
### 字典dict
# 补充:
# 不可变类型=》可hash类型
# 可变类型 =》不可以hash类型
#一:基本使用dict
# 1 用途:记录多个值,每一个值都对应的key用来描述value的作用
# 2 定义方式:在{}内用逗号分隔开多 个key:value,其中value可以是任意类型,而key必须是不可变的类型,通常情况下应该str类型
dic={0:‘aaa‘,1:‘bbb‘,2:‘cccc‘} # dic=dict({0:‘aaa‘,1:‘bbb‘,2:‘cccc‘}) print(dic,type(dic)) print(dic[0])
dic={[1,2]:‘aaa‘} dic={(1,2):‘aaa‘} print(dic[(1,2)])
# 用法一:
d = dict(a=1,b=2,c=3) print(d) {‘a‘: 1, ‘b‘: 2, ‘c‘: 3}
# 用法二:
userinfo = [ [‘name‘,‘qqq‘], [‘age‘, 88], [‘sex‘,‘male‘] ] d = {} for k,v in userinfo: print(k,v) d[k] = v print(d)
#优先掌握的操作:
#1、按key存取值:可存可取
dic={‘name‘:‘egon‘} print(dic[‘name‘]) dic[‘name‘]=‘EGON‘ print(dic) dic[‘age‘]=18 print(dic)
#2、长度len
dic={‘name‘:‘egon‘,‘age‘:18,‘name‘:‘EGON‘,‘name‘:‘XXXX‘} print(dic) print(len(dic))
#3、成员运算in和not in:字典的成员运算判断的是key
###3、成员运算in和not in:字典的成员运算判断的是key d3 = {‘name‘:‘egon‘,‘age‘:18,} print(‘name‘ in d3)print(‘age‘ in d3) #4 删除 dic = {‘name‘:‘egon‘, ‘age‘:18} del dic[‘name‘] print(dic) res = dic.pop(‘age‘)print(res) #5. 键keys(), 值 values,键值对 items, 注意 python2 与 python3 之间的区别
dic = {‘name‘:‘egon‘, ‘age‘:88} print(dic.keys()) l = [] for k in dic.keys(): l.append(k) print(l) print(list(dic.keys())) print(dic.values()) print(dic.items()) 返回结果;
[‘name‘, ‘age‘]
[‘name‘, ‘age‘]
dict_values([‘egon‘, 88])
dict_items([(‘name‘, ‘egon‘), (‘age‘, 88)])
#6、循环
dic = {‘name‘:‘egon‘, ‘age‘:18, ‘sex‘:‘male‘} for k in dic.keys(): print(k, dic[k]) for k in dic: print(k, dic[k]) for v in dic.values(): print(v) for k,v in dic.items(): print(k, v) 返回结果: name egon age 18 sex male name egon age 18 sex male egon 18 male name egon age 18 sex male
###8 dict.get() print(dic.get(‘name‘)) print(dic.get(‘xxx‘)) ### 如果K 不存在,返回 None
# 需要掌握的操作:
# setdefault: 有则不动/返回原值,无则添加/返回新值 dic2 = { ‘name‘:‘egon‘, ‘age‘:18print(dic2.setdefault(‘name‘,‘ooo‘)) 返回:egon
dic2 = { ‘name‘:‘egon‘, ‘age‘:18}print(dic2.setdefault(‘name‘,‘ooo‘)) #返回:egonprint(dic2.setdefault(‘sex‘,‘male‘)) #返回:male
## 集合set
pythons=[‘李二丫‘,‘张金蛋‘,‘李银弹‘,‘赵铜蛋‘,‘张锡蛋‘,‘alex‘,‘oldboy‘]
linuxs=[‘lxx‘,‘egon‘,‘张金蛋‘,‘张锡蛋‘,‘alex‘,‘陈独秀‘]
l=[]
for stu in pythons:
if stu in linuxs:
l.append(stu)
print(l)
#一:基本使用:set
# 1 用途: 关系运算,去重
#
# 2 定义方式: 在{}内用逗号分开个的多个值
# 集合的三大特性:
# 2.1 每一个值都必须是不可变类型
# 2.2 元素不能重复
# 2.3 集合内元素无序
s={1,3.1,‘aa‘,(1,23),} # s=set({1,3.1,‘aa‘,(1,23),})
print(s,type(s))
s={1,1,1,1,1,1,1,1,1,2,3}
print(s)
s={‘a‘,‘b‘,‘c‘}
s[0]
s=set(‘hello‘)
print(s)
print(set([‘a‘,‘b‘,‘c‘,[1,2]]))
# 3 常用操作+内置的方法
pythons={‘李二丫‘,‘张金蛋‘,‘李银弹‘,‘赵铜蛋‘,‘张锡蛋‘,‘alex‘,‘oldboy‘}
linuxs={‘lxx‘,‘egon‘,‘张金蛋‘,‘张锡蛋‘,‘alex‘,‘陈独秀‘}
# 取及报名python课程又报名linux课程的学员:交集
print(pythons & linuxs)
print(pythons.intersection(linuxs))
# 取所有报名老男孩课程的学员:并集
print(pythons | linuxs)
print(pythons.union(linuxs))
# 取只报名python课程的学员: 差集
print(pythons - linuxs)
print(pythons.difference(linuxs))
# 取只报名linux课程的学员: 差集
print(linuxs - pythons)
print(linuxs.difference(pythons))
# 取没有同时报名两门课程的学员:对称差集
print(pythons ^ linuxs)
print(pythons.symmetric_difference(linuxs))
# 是否相等
s1={1,2,3}
s2={3,1,2}
print(s1 == s2)
# 父集:一个集合是包含另外一个集合
s1={1,2,3}
s2={1,2}
print(s1 >= s2)
print(s1.issuperset(s2))
s1={1,2,3}
s2={1,2,4}
print(s1 >= s2)
# 子集
s1={1,2,3}
s2={1,2}
print(s2 <= s1)
print(s2.issubset(s1))
# 需要掌握操作
s1={1,2,3}
s1.update({3,4,5})
print(s1)
s1={1,2,3}
res=s1.pop()
print(res)
s1={1,2,3}
res=s1.remove(3) #单纯的删除,返回值为None
print(s1)
s1={1,2,3}
s1.add(4)
print(s1)
s1={1,2,3}
s2={1,2}
s1.difference_update(s2) #s1=s1.difference(s2)
print(s1)
s1={1,2,3}
res=s1.discard(3) ##单纯的删除,返回值为None
print(s1)
print(res)
s1.remove(444444) #删除的元素不存在则报错
s1.discard(444444) #删除的元素不存在不会报错
s1={1,2,3}
s2={1,2,4}
print(s1.isdisjoint(s2)) #如果两个集合没有交集则返回True
s1={1,2,3}
s2={4,5,6}
print(s1.isdisjoint(s2)) #如果两个集合没有交集则返回True
#二:该类型总结
# 1 存多个值
#
# 2 无序
#
# 3 set可变
s={1,2,3}
print(id(s))
s.add(4)
print(id(s))
# 集合去重
# 局限性
#1、无法保证原数据类型的顺序
#2、当某一个数据中包含的多个值全部为不可变的类型时才能用集合去重
names=[‘alex‘,‘egon‘,‘alex‘,‘alex‘,‘egon‘,‘lxx‘]
s=set(names)
print(s)
l=list(s)
print(l)
stus_info=[
{‘name‘:‘egon‘,‘age‘:18},
{‘name‘:‘alex‘,‘age‘:73},
{‘name‘:‘oldboy‘,‘age‘:84},
{‘name‘: ‘egon‘, ‘age‘: 18},
{‘name‘: ‘egon‘, ‘age‘: 18},
{‘name‘: ‘egon‘, ‘age‘: 18},
{‘name‘: ‘oldboy‘, ‘age‘: 84},
]
# set(stus_info) # 报错
l=[]
for info in stus_info:
if info not in l:
l.append(info)
# print(l)
stus_info=l
print(stus_info)
原文地址:https://www.cnblogs.com/chendaodeng/p/11134931.html