python入门学习篇七
作者:互联网
列表的内置方法
# l1 = [11, 22, 33, 44, 55, 66] # l1.reverse() # print(l1) # l1 = [99, 11, 22, 33, 44, 55, 66] # l1.sort(reverse=True) # 默认是升序排列 # print(l1) # 列表比较 # l1 = [999, 888] # l2 = [111, 222, 333, 444, 555] # print(l1 > l2) # 列表切片 l = [1, 2, 3, 4, 5, 6, 7, 8, 9] # print(l[1]) # print(l[1:5]) # print(l[1:]) # 冒号右边不写,代表从开始位置一直切到末尾 # print(l[:5]) # 冒号左边不写,代表从头开始一直切断索引指定位置 # print(l[1:8:2]) # # print(l[-1]) # -1位置取得就是末尾数据 # print(l[-8:-1:2]) # print(l[::-1]) # 冒号左右两边都不写,代表全都要 # s = 'helloworld' # print(s[1:]) # print(s[:6])
字典的内置方法
# 1. 定义字典 d = {'username': 'ly', 'age': 12} # 2. 第二种方式dict # d1 = dict(name='ly', age=18, gender='male') # print(d1) # 了解 # info = dict([['name', 'tony'], ('age', 18)]) # print(info) # dic = { # 'name': 'xxx', # 'age': 18, # 'hobbies': ['play game', 'basketball'] # } # 1. 取值 # print(dic['name1111']) # print(dic['hobbies']) # 2. 第二种方式, 掌握 # print(dic.get('name1111', 666)) # None # print(dic.get('name', 666)) # None # print(dic.get('hobbies')) # 3. 修改值 # dic['name'] = 'ly' # k值 存在,直接进行修改操作 # dic['pwd'] = '123456' # k值不存在,会往字典中添加一个k:v # print(dic) # l = [1, 2, 3] # 0-2 # # l[4] = 666 # print(l[4]) # 4. 求长度 # l = [1, 2, 3, 4, 5, 6] # print(len(l)) # print(len(dic)) # 5. 成员运算 # print('name' in dic) # print('name' not in dic) # 6. 删除 # 第一种方式 # del dic['name'] # del dic['hobbies'] # print(dic) # 第二种方式 # dic.pop('name') # dic.pop('hobbies') # print(dic) dic = { 'name': 'xxx', 'age': 18, 'hobbies': ['play game', 'basketball'] } # 7. 字典三剑客 # print(dic.keys()) # dict_keys(['name', 'age', 'hobbies']) => 列表 # print(dic.values()) # dict_values(['xxx', 18, ['play game', 'basketball']]) => 列表 # print(dic.items()) # dict_items([('name', 'xxx'), ('age', 18), ('hobbies', ['play game', 'basketball'])]) # 8. 循环字典 # for i in dic: # print(i) # print(dic[i]) # print(dic.get(i)) # [('name', 'xxx'), ('age', 18), ('hobbies', ['play game', 'basketball'])] # k,v = ('name', 'xxx') # for k, v in dic.items(): # print(k, v) for i in dic.keys(): print(i) for j in dic.values(): print(j) 字典需要了解的方法 dic = { 'name': 'xxx', 'age': 18, 'hobbies': ['play game', 'basketball'] } # print(dic.popitem()) # print(dic) # dic1 = { # 'name':'aaa' # } # dic1.update({'name': 'ly', 'pwd': 123}) # print(dic1) # dic1 = dict.fromkeys(['k1', 'k2', 'k3'], []) # print(dic1) # dic1['k1'] = [] # dic1['k1'].append(666) # # dic1['k1'].append(777) # # dic1['k1'].append(888) # print(dic1) # setdefault # print(dic.setdefault('name1111', 666)) # print(dic)
元组的内置方法
# 1.类型转换 关键字:tuple tuple(111) # 不行 tuple(1.11) # 不行 tuple('helloworld') # 行 ... # 支持for循环的数据类型都可以转为元组 # 第一道笔试题: t1 = (111) t2 = (1.22) t3 = ('helloworld') t4 = ('a', 'b') t5 = ('c', ) '''当元组中只有一个元素的时候,也要加逗号''' print(type(t1)) # <class 'int'> print(type(t2)) # <class 'float'> print(type(t3)) # <class 'str'> print(type(t4)) # <class 'tuple'> print(type(t5)) # <class 'tuple'> # 求长度 len(tuple1) # 第二道笔试题 t1 = (111, 222, [444, 555, 666]) # print(t1) # print(t1[2]) t1[2].append(777) print(t1)
集合的内置方法
而集合类型既没有索引也没有key与值对应,所以无法取得单个的值,而且对于集合来说,主要用于去重与关系元素,根本没有取出单个指定值这种需 d = {} # 默认是空字典 s = set() # 这才是定义空集合 # 第二道题:去重,并且保留原来的顺序 ll = [11, 22, 22, 22, 33, 33, 44, 44, 55, 66, 77, 77, 88] # ss = set(ll) # l1 = list(ss) # print(l1) new_list = [] for i in ll: if i not in new_list: new_list.append(i) print(new_list)
集合的运算关系
friends1 = {"zero", "kevin", "jason", "egon"} friends2 = {"Jy", "ricky", "jason", "egon"} # 求合集,并集 print(friends1 | friends2) # 求交集 print(friends1 & friends2) # 求friends1差集 print(friends1 - friends2) # 求friends2差集 print(friends2 - friends1) # 求对称差集 print(friends1 ^ friends2) # 求父集,子集 print(friends1 > friends2) print(friends1 < friends2)
搜索
复制
标签:入门,python,l1,dic,学习,dic1,hobbies,print,name 来源: https://www.cnblogs.com/zhangrukai/p/15748970.html