006 python 元组tuple 字典dict 集合set
2019-03-25 21:33
309 查看
元组 tuple
#元组的学习 # 基本使用 # 作用:存多个值 # 定义:使用 () 定义,括号内的元素用逗号隔开 ,也可以使用括号来定义,不推荐 # tup1 = (1,2,3,4,5,6) # print(type(tup1)) # tup2 = (1,2,3,'1','2','3') # print(type(tup2)) # tup3 = '1','2','3',1,2,3 # print(type(tup3)) # tup4 = "1","2","class" # print(type(tup4)) # tup5 = 1,2,3,4,5 # print(type(tup5)) # tup6 = [1,2,3],[4,5,6],[7,8,9] # print(type(tup6)) # tup7 = ('class',(1,2,3,4),'clas',('1',1,'2')) # print(type(tup7)) # 如果只有一个元素 要使用逗号分隔 # tup8 = (1) # print(type(tup8)) # 输出int类型 # tup9 = (1,) # print(type(tup9)) # 输入tuple类型 # 取值 # 1.索引取值 # 正向取 # print(tup1[0]) # print(tup1[3]) # print(tup4[2]) # print(tup6[2]) # 反向取 # print(tup1[-1]) # print(tup4[-2]) # print(tup7[-3]) # 2.切片取 顾头不顾尾步长默认1 # print(tup1[1:]) # print(tup7[::-1]) # print(tup4[:2]) # print(tup2[:4:2]) # 3.元组的更新 元组属于不可变的类型,无法直接增/删/改 # 改-重新赋值一个元祖给它 # print(tup1) # tup1 = ('t1',2) # print(type(tup1)) # print(tup1) # 删-只能用del()函数 但是它会直接删除定义的元组,程序运行会报错 # print(tup2) # del(tup2) # print(tup2) # 增-使用+号来合并两个元祖 # print(tup1+tup2) # print(tup2+tup6) # len() 取元组内元素的个数 # print(len(tup1)) # print(len(tup4)) ''' 运算符 len() 取元组内元素的个数 + 连接两个元组 * 复制元组 in 元素是否存在 not in 元素是否不存在 for 循环遍历出元素 ''' # * # tup11 = (1,2,3) # print(tup11 * 2) # in not in # tup = (1,2,3,4) # print(1 in tup) # print(5 in tup) # print(1 not in tup) # print(8 not in tup) # for # tup = 1,2,3,4,5,6 # for i in tup: # print(i) ''' 常用的一些函数 max() 取元素最大值 min() 取元素最小值 tuple() 将列表转换为元组 count() 统计元素出现的次数 index() 查找元素的位置 ''' # tup = 1,2,3,4,5 # print(max(tup)) # print(min(tup)) # tups = '1','2','3','4' # print(max(tups)) # print(min(tups)) # tupsi = '1','x','3','4' # print(max(tupsi)) # print(min(tupsi)) # str1 = 'bran' # list1 = [1,2,3,4,5] # print(tuple(str1)) # print(tuple(list1)) # dict1 = {'key':1,'key1':3} # 只能将键转出 # print(tuple(dict1)) # tup = 1,2,1,4,5 # print(tup.count(1)) # print(tup.index(4)) # 类型的总结 # 元组和列表的区别 # 相同点:可以存储多个不同类型的值,可迭代对象 # 不同点:1.元组定义使用括号,列表使用方括号 2.元组只能取不能修改,列表可以 # 不可变,有序,存多个值 ''' 购物车 ''' # msg_dic={ # 'apple':10, # 'tesla':100000, # 'mac':3000, # 'lenovo':30000, # 'chicken':10,} # goods = [] # while True: # # 打印商品出来 # for k,v in msg_dic.items(): # print('商品:{} 价格:{}'.format(k,v)) # # 输入商品名 # choice = input('input shop name').strip() # # 判断商品是否存在 # if choice not in msg_dic:continue # # 输入个数 # count = input('input num >>').strip() # # 判断输入的不是整数 直接返回 # if not count.isdigit():continue # goods.append((choice,msg_dic[choice],count)) # print(goods)
字典 dict
''' 字典的学习 用途:存多个任意类型的值 以键值对来存取值 取值的速度快 定义:使用 {} 括号来定义,多个键值对要用逗号来分割,键是唯一的,不可变的,但值可以是任何类型 增/删/改/查/转 常用的方法 总结: 1.字典的键必须是唯一的,不能重复,如果重复字典只记住最后面的key 2.key是不可变的,所以可以是数字,字符串,元组。列表是不可以的 ''' # 定义 # d = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'} # dict1 = {'name':'bran','age':12,'aihao':['code','play'],'info':(19990101,'man')} # dict_int = {1:'bran',2:123,(1,2,3):12} # print(dict_int) # 查 就是取值 通过key取值 # print(dict1['name']) # print(dict1['info']) # print(dict1['age']) # 改 通过键来修改 # dict1['name'] = 'jace' # print(dict1['name']) # dict1['aihao'][0] = 'ppp' # print(dict1) # 删 通过key来删除 clear() 清空 # del(dict1['name']) #通过key删除值 # print(dict1) # dict1.clear() # print(dict1) # del(dict1) # 直接删除字典 # print(dict1) # 改 update()追加另一个字典数据到尾部 # dict2 = {'xx':12,22:'222'} # dict1.update(dict2) # print(dict1) # dict1.update({'xxx':123,'123':123}) # print(dict1) # 常用的函数 # len() 返回字典的键值对数量 # print(len(dict1)) # str() 将字典转为字符串类型 # print(type(str(dict1))) # copy() 复制一份给另一个字典 就是重新创建了一个新的字典 # dict2 = {'name':'bran','age':123} # dict3 = dict2.copy() # print(dict3) # copy() 和 直接赋值的却别 # dict1 = {'user':'runoob','num':[1,2,3]} # dict2 = dict1 # 浅拷贝: 引用对象 # dict3 = dict1.copy() # 深拷贝:深拷贝父对象(一级目录),子对象(二级目录)不拷贝,还是引用 # # 修改 data 数据 # dict1['user']='root' # dict1['num'].remove(1) # # 输出结果 # print(dict1) # print(dict2) # print(dict3) ''' 直接赋值,浅拷贝,深拷贝 ''' # import copy # a = [1,2,3,4,['a','b']] # 直接赋值给b 其实就是把a 的内存地址给了b # b = a # print(id(a)) # print(id(b)) # 如果修改了a里面的值,b里面的值也是改变了,因为它们都是指向同一个内存地址 # a.append(5) # a[-2].append('c') # print(a) # print(id(a)) # print(b) # print(id(b)) # copy() 浅拷贝 只拷贝第一层数据 也就是父对象 # c = a.copy() # print(a) # print(c) # print(id(a)) # print(id(c)) # 修改第一层数据的值,a和c都是互相不想干的 # a.append(5) # print(a) # print(c) # 修改第二层数据的值,a和c的值都是被修改的 ,因为第二层的id地址相等 # a[-1].append('c') # print(a) # print(c) # print(id(a[-1])) # print(id(c[-1])) # copy.deelcopy() 深拷贝 复制所有层的数据给一个新的变量 # c = copy.deepcopy(a) # print(id(a)) # 完全是两个不同的ID地址 # print(id(c)) # 修改a的第一层值 c的值没有改变 # a.append(4) # print(a) # print(c) # 修改a的第二层值 c的第二层值也是没有改变 # a[-1].append('c') # print(a) # print(c) # 第二层值的id都是不一样的所以值没有改变 # print(id(a[-1])) # print(id(c[-1])) # fromkeys(seq,[value]) 创建一个新的字典,将序列做参数传进来然后转为key,value的默认值是None, # seq = 'key1','key2','key3' # print(dict.fromkeys(seq)) # print(dict.fromkeys(seq,'value')) # get(key,default=None) 返回指定键的值,如果指定的键没有在字典中则返回默认值None # dict1 = {'name':'bran','age':12,'aihao':['code','play'],'info':(19990101,'man')} # print(dict1.get('name')) # print(dict1.get('name1')) # print(dict1.get('name1',0)) # in not in 成员运算 判断key是否存在字典中 # print('bran' in dict1) # print('name' in dict1) # print('name' not in dict1) # print('xx' not in dict1) # items() 以列表返回可遍历的(键值)元组数组 # dict1 = {'name':'bran','age':12,'aihao':['code','play'],'info':(19990101,'man')} # print(dict1.items()) # keys() 返回一个可迭代的对象,可使用list转列表,⚠️在python2中直接返回列表 # dict1 = {'name':'bran','age':12,'aihao':['code','play'],'info':(19990101,'man')} # print(dict1.keys()) # setdefault(key,default=None) 和get类似,不过如果键不存在,则添加键到字典值为默认值 # dict1 = {'name': 'bran', 'age': 12, 'aihao': [ # 'code', 'play'], 'info': (19990101, 'man')} # print(dict1.setdefault('name', 'jane')) # dict1.setdefault('xx', 'xxx') # print(dict1) # update(dict) 将一个字典添加到字典中 # dict1 = {'name':'bran','age':12,'aihao':['code','play'],'info':(19990101,'man')} # dict2 = {'xx':'pppp'} # dict1.update(dict2) # print(dict1) # values() 返回一个迭代器 可以使用list来转列表 # dict1 = {'name':'bran','age':12,'aihao':['code','play'],'info':(19990101,'man')} # print(dict1.values()) # pop(key,[default]) 删除字典指定的键所对应的值,返回被删除的字典键值对,key必须给。否则返回default # dict1 = {'name': 'bran', 'age': 12, 'aihao': [ # 'code', 'play'], 'info': (19990101, 'man')} # print(dict1.pop('name')) # print(dict1) # print(dict1.pop('name','No')) # print(dict1) # popitem() 随机返回并删除字典中的一对键值(一般删除末尾部) # print(dict1) # print(dict1.popitem()) # print(dict1) # 循环for # dict1 = {'name':'bran','age':12,'aihao':['code','play'],'info':(19990101,'man')} # for d in dict1: # print(d) # print('-'*10) # for k,v in dict1.items(): # print(k,v) # print('-' * 10) # for k in dict1.keys(): # print(k) # print('-' * 10) # for v in dict1.values(): # print(v) # 练习 # 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中 # list1 = [11,22,33,44,55,66,77,88,99,] # dict1 = {'k1':[],'k2':[]} # for i in list1: # if i > 66: # dict1['k1'].append(i) # else: # dict1['k2'].append(i) # print(dict1) # 统计s='hello alex alex say hello sb sb'中每个单词的个数 # s = 'hello alex alex say hello sb sb' # list1 = s.split(' ')/ # dict1 = {} # for i in list1: # if i in dict1: # dict1[i] += 1 # else: # dict1[i] = 1 # print(dict1) # for i in list1: # dict1.update({i:list1.count(i)}) # dict[i] = list1.count(i) # print(dict1) # for i in list1: # dict1.setdefault(i,list1.count(i)) # print(dict1) # set1 = set(list1) # print(type(set1)) # print(set1) # for i in set1: # dict1[i]=list1.count(i) # print(dict1)
集合 set
''' 集合set 无序的,不重复的元素列表 用途:去重,关系运算 定义:使用set()函数创建,也可以使用{}创建,不过创建空的集合要用set()因为花括号用来创建字典的,自带去重功能 元素遵循的原则:1.每个元素必须是不可变类型 2.没有重复的元素 3.无序 总结:集合的目的不是将不同的值存放在一起,不同的集合间用来做关系运算 ''' # 定义 # set1 = {'1','2','3','5'} # set2 = set() # print(type(set1)) # print(type(set2)) # print(set1) # print(set2) # 去重 # basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} # print(basket) # | 合集 合并两个集合,并去重 同函数union() # set1 = set('abcdefg') # set2 = set('abcdzxcv') # print(set1 | set2) # set1.union(set2) # print(set.union(set1,set2)) # & 交集 取两个集合内都存在的元素 同函数intersection() # print(set1 & set2) # print(set.intersection(set1,set2)) # - 差集 取一个集合有的,而另一个集合内没有的 同函数difference() # print(set1 - set2) # 取set2内不存在set1的值 # print(set2 - set1) # 取set1内不存在set2的值 # print(set.difference(set1,set2)) # ^ 对称差集 取两个集合内不对称的值 # print(set1 ^ set2) # == 比较两个集合是否相等 # set3 = set('abc') # set4 = set('abc') # print(set1 == set2) # print(set3 == set4) # 父集:>,>= 判断小于的每个元素是否存在大于的元素中 # set3 = set('abc') # set4 = set('abc') # print(set3 >= set4) # 判断set4中的每个元素是否都在set3中 # 子集:<,<= # print(set3 <= set4) # 判断set3中的没有元素是否都在set4中 # add() 单个元素的添加 # st = set('abcdefg') # print(st) # st.add('dd') # print(st) # update() 可添加一个集合 或 多个元素 用逗号隔开 # st.update({2,3,4}) # print(st) # st.update('1','2') # print(st) # remove() 删除元素 如果元素不存在则报错 # st.remove('a') # print(st) # discard() 删除元素 如果元素不存在 不报错 # st.discard('aa') # print(st) # pop() 随机删除集合内的元素 默认删除排序后的第一个元素 # print(st) # st.pop() # print(st) # clear() 清空元素 # st.clear() # print(st) # len() 取集合内元素的个数 # print(len(st)) # in not in 判断元素是否在集合内 # print('a' in st) # print('dd' not in st) # 练习 ''' 有如下两个集合,pythons是报名python课程的学员名字集合,linuxs是报名linux课程的学员名字集合 1. 求出即报名python又报名linux课程的学员名字集合 2. 求出所有报名的学生名字集合 3. 求出只报名python课程的学员名字 4. 求出没有同时这两门课程的学员名字集合 ''' # pythons = {'alex','egon','yuanhao','wupeiqi','gangdan','biubiu'} # linuxs = {'wupeiqi','oldboy','gangdan'} # 1 # print(pythons & linuxs) # print(set.intersection(pythons,linuxs)) # 2 # print( pythons | linuxs) # print(set.union(pythons,linuxs)) # 3 # print(pythons - linuxs) # 4 # print(pythons ^ linuxs) ''' 二.去重 1. 有列表l=['a','b',1,'a','a'],列表元素均为可hash类型,去重,得到新列表,且新列表无需保持列表原来的顺序 2.在上题的基础上,保存列表原来的顺序 3.去除文件中重复的行,肯定要保持文件内容的顺序不变 4.有如下列表,列表元素为不可hash类型,去重,得到新列表,且新列表一定要保持列表原来的顺序 ''' # 1 # l = ['a','b',1,'a','a'] # print(set(l)) # 2 # l1 = ['a','b',1,'a','a'] # l = [] # for i in l1: # if i not in l: # l.append(i) # print(l) # s = set() # for i in l1: # if i not in s: # s.add(i) # l.append(i) # print(l) # l=[ # {'name':'egon','age':18,'sex':'male'}, # {'name':'alex','age':73,'sex':'male'}, # {'name':'egon','age':20,'sex':'female'}, # {'name':'egon','age':18,'sex':'male'}, # {'name':'egon','age':18,'sex':'male'}, # ] # s = set() # ll = [] # for item in l: # val = (item['name'],item['age'],item['sex']) # if val not in s: # s.add(val) # ll.append(item) # print(ll)
相关文章推荐
- 003——Python内建数据结构列表(list)、元组(tuple)、字典(dict)、集合(set)
- python---元组tuple,集合set,字典dict
- python3基础: 元组tuple、 列表list、 字典dict、集合set。 迭代器、生成器
- python 列表list 元组tuple 字典dict 集合set 介绍
- Python学习笔记(9)-列表list、元组tuple、字典dict、集合set
- python的列表(List),元组(Tuple),集合(Set),字典(Dict)
- Python中的列表(list),元组(Tuple),字典(Dict)和集合(Set)
- python之列表(list),元组(tuple),集合(set),字典(dict)
- Python中的列表(list),元组(Tuple),字典(Dict)和集合(Set)
- python 列表list 元组tuple 字典dict 集合set 介绍
- 字典(dict),列表(list),元组(tuple),集合(*set)的基本概述
- python基础数据结构——列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 介绍及相互转换
- 列表(list)、元组(tuple)、字典(dict)和集合(set)的主要区别
- 字典(dict),元组(tuple),集合(set),列表(list)知识点补充
- Python入门笔记—第五章【元祖tuple,集合set,字典dict】
- Python3学习笔记【基础3--list,tuple,if条件判断,for/while循环,dict字典,set集合】
- python中的集合详解(列表List,元组Tuple,字典,set)
- python基础学习——列表(list), 元祖(tuple), 字典(dict), 字符串(string), 集合(set) 介绍及相互转换
- 元组(tuple)、列表(list)、集合(set)和字典(dict)理解
- 【Python】基础学习——列表(list),元祖(tuple),字典(dict),字符串(string),集合(set)