python的元组,列表,字典,集合大综合
元组
元组每个元素都有它的索引,并且内置确定最大最小元素的方法。它不能修改,创建的时候用圆括号(或者不用括号)列表用方括号。元组中的元素用逗号分隔开,当元组中有一个元素时末尾要用逗号分隔开
>>>tup1 = ('Google', 'Runoob', 1997, 2000); >>> tup2 = (1, 2, 3, 4, 5 ); >>> tup3 = "a", "b", "c", "d"; # 不需要括号也可以 >>> type(tup3) <class 'tuple'>
元组的下标从0开始,可以通过索引对元组进行访问,
#!/usr/bin/python3 tup1 = ('Google', 'Runoob', 1997, 2000) tup2 = (1, 2, 3, 4, 5, 6, 7 ) print ("tup1[0]: ", tup1[0]) print ("tup2[1:5]: ", tup2[1:5]) 结果: tup1[0]: Google tup2[1:5]: (2, 3, 4, 5)
元组里的元素是没办法修改的,但是可以对元组进行连接组合
#!/usr/bin/python3 tup1 = (12, 34.56); tup2 = ('abc', 'xyz') # 以下修改元组元素操作是非法的。 # tup1[0] = 100 # 创建一个新的元组 tup3 = tup1 + tup2; print (tup3) 结果: (12, 34.56, 'abc', 'xyz')
元组中元素的值不允许删除,但是可以通过del来删除整个元组
#!/usr/bin/python3 tup = ('Google', 'Runoob', 1997, 2000) print (tup) del tup; print ("删除后的元组 tup : ") print (tup) 结果:删除后的元组 tup : Traceback (most recent call last): File "test.py", line 8, in <module> print (tup) NameError: name 'tup' is not defined
元组还可以复制,迭代,判断元素是否存在以及正反向读取,截取元素。
tup1 = (1, 2, 3, (4), [5], {6}) if 2 in tup1: #判断元素是否存在 print('2存在') else: print('不存在') tup2 = (2, 3, 4, 5, 6) for i in tup2: print(i) #元组的迭代 tup3 = ('aa',)*4 # 数组的复制 print(tup2[-2]) # 读取倒数第二个元素 print(tup2[2]) # 读取第三个元素 print(tup2[1:]) # 截取第二个之后的所有元素 print(tup3) print(tup1+tup2) print(tup1) 结果: 2存在 2 3 4 5 6 5 4 (3, 4, 5, 6) ('aa', 'aa', 'aa', 'aa') (1, 2, 3, 4, [5], {6}, 2, 3, 4, 5, 6) (1, 2, 3, 4, [5], {6})
列表
列表中的截取组合访问跟元组类似,关键点在于更新的时候,列表有append()方法可以用,将需要增加的元素加在列表的最后,如果要改变列表中某个元素的值可以直接取出需要改变的元素并给其赋值。
lis = [1,2,3,9,8,6,7,5,4,3] a = lis.count(3)# 统计元素在列表中出现的次数 print(a) #2 #lis1 = [3,4] lis.extend([3,4]) #在元素的末尾一次性追加另一个序列中的多个值 print(lis) #[1, 2, 3, 9, 8, 6, 7, 5, 4, 3, 3, 4] b = lis.index(2) # 找出元素第一个匹配项的索引值 print(b) #1 lis.insert(2,10) # 在列表的指定位置插入指定元素 print(lis) #[1, 2, 10, 3, 9, 8, 6, 7, 5, 4, 3, 3, 4] lis.pop() # 移除列表中的元素默认是最后一个 print(lis) #[1, 2, 10, 3, 9, 8, 6, 7, 5, 4, 3, 3] lis.remove(2) # 移除列表中某个值的第一个匹配项 print(lis) # [1, 10, 3, 9, 8, 6, 7, 5, 4, 3, 3] lis.reverse() # 反向列表中的元素 print(lis) #[1, 10, 3, 9, 8, 6, 7, 5, 4, 3, 3] lis.sort() # 默认递增排序 print(lis) #[3, 3, 4, 5, 7, 6, 8, 9, 3, 10, 1] #lis.clear()# 清空列表 #print(lis) #[] lis.copy() print(lis) # [1, 3, 3, 3, 4, 5, 6, 7, 8, 9, 10] # [1, 3, 3, 3, 4, 5, 6, 7, 8, 9, 10]
在这我踩过一个坑,在迭代删除列表元素的时候可能跟预想的结果不一致,
test = ['a','','b','','c','',''] for i in test: if i == '': test.remove(i) print(i) print(test) 结果: a ['a', 'b', 'c', '']
就例如这段代码,本以为可以把‘ ’全部移除但其实则不然,原因就在于for循环按照索引进行遍历,但是一旦执行一次remove元素就移走一个,原来列表的元素全部向前移动一位,这样一来原来索引指向‘ ’的时候满足条件‘ ’移走了,b取代了 ‘ ’的位置,但是这时候执行for循环索引加1指向b后边的那个‘ ’直接跳过了b,b也不会打印。
Test是Python List对象,用for循环遍历,内部维护了每次遍历的索引,执行一次,索引+1,假如有5个元素,索引是从0-4,每次+1,直到遍历结束,
第四次循环,看图得知,for又指向空字符串,和上次循环同理,remove
第五次循环,,,没了,元素全部用完了,在这要注意的是remove是删除第一个为某个元素的方法。
当然这只是注意事项,我们有很多方法来替代例如直接转化成集合,可以直接去重,如果想在li2列表中去掉li列表的元素,可以先copy一份li遍历li中的元素,操作li1中元素
li = [1,2,4,5,7,8] li1 = [1,2,4,5,7,8] li2 = [1,3,5,7,'a','s','d'] for i in li: if i in li2: li1.remove(i) print(li1) 结果: [2, 4, 8]
还有一种办法是倒序遍历列表,元素向前移动完全不影响,巧妙
a = [1,3 ,'c', 6,'c',8 ,'c',9 ,'c'] for i in reversed(a): if i=='c': a.remove(i) print(a) #[1, 3, 6, 8, 9]
字典
字典是一种可变的容器模型,可以存储任意类型的对象,键不必唯一但是值必须要唯一,它的值可以取任何数据类型,但是键必须是不可变的,字符串数字或者元组。
简单的字典:
dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
f访问字典里边的值:
#!/usr/bin/python3 dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'} print ("dict['Name']: ", dict['Name']) print ("dict['Age']: ", dict['Age']) 结果: dict['Name']: Runoob dict['Age']: 7
修改字典:
#!/usr/bin/python3 dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'} dict['Age'] = 8; # 更新 Age dict['School'] = "菜鸟教程" # 添加信息 print ("dict['Age']: ", dict['Age']) print ("dict['School']: ", dict['School']) 结果: dict['Age']: 8 dict['School']: 菜鸟教程
删除字典的元素:
#!/usr/bin/python3 dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'} del dict['Name'] # 删除键 'Name' dict.clear() # 清空字典 del dict # 删除字典 print ("dict['Age']: ", dict['Age']) print ("dict['School']: ", dict['School']) 但这会引发一个异常,因为用执行 del 操作后字典不再存在 Traceback (most recent call last): File "test.py", line 9, in <module> print ("dict['Age']: ", dict['Age']) TypeError: 'type' object is not subscriptable
最后笔者要说一点就是关于字典的元素删除,如果想删除里边的某个元素的话可能会想到迭代然后比较看是否存在这样的元素,存在就删除,但是会报错。正确的做法是可以将不需要删除元素单独放一起,然后就可以得到想要的元素了。或者是把需要删除的元素放到列表里,再迭代删除列表。
#错误 dir = {1:'a',2:'w',3:'e',4:'r',5:'t',6:'y',7:'u'} for i in dir: if i ==2: del dir[i] print(dir) #报错 Traceback (most recent call last): File "F:/third/tttt.py", line 101, in <module> for i in dir: RuntimeError: dictionary changed size during iteration
正确 dir2={} dir = {1:'a',2:'w',3:'e',4:'r',5:'t',6:'y',7:'u'} for i in dir: if i == 2: pass else: dir2.setdefault(i,dir[i]) dir = dir2 print(dir) #{1: 'a', 3: 'e', 4: 'r', 5: 't', 6: 'y', 7: 'u'}
由于它的方法比较多所以不在这举例,可以参看:
http://www.runoob.com/python3/python3-dictionary.html
集合
集合是不重复而且无序的元素序列,所以在去重的时候可以先将需要去重的数据转化成集合,集合会自动去掉重复元素。集合之间还可以求交集和并集反交集和差集等
集合添加元素可以是用add(x)或者update(x),update添加的元素可以有多个用‘,’分隔开,可以是列表元组字典等。
thisset = set(("Google", "Runoob", "Taobao")) thisset.add("Facebook") print(thisset) #{'Taobao', 'Facebook', 'Google', 'Runoob'} thisset = set(("Google", "Runoob", "Taobao")) thisset.update({1,3}) print(thisset) #{1, 3, 'Google', 'Taobao', 'Runoob'} thisset.update([1,4],[5,6]) print(thisset) #{1, 3, 4, 5, 6, 'Google', 'Taobao', 'Runoob'}
移除元素可以用remove(x)但是元素如果不存在的话会发生错误,另外一个方法discard(x),如果元素不存在不发生错误,pop(x)就是随机的删除一个元素。
thisset = set(("Google", "Runoob", "Taobao")) thisset.remove("Taobao") print(thisset) #{'Google', 'Runoob'} thisset.remove("Facebook") # 不存在会发生错误 #Traceback (most recent call last): # File "<stdin>", line 1, in <module> #KeyError: 'Facebook'
thisset = set(("Google", "Runoob", "Taobao")) thisset.discard("Facebook") # 不存在不会发生错误 print(thisset) #{'Taobao', 'Google', 'Runoob'}
thisset = set(("Google", "Runoob", "Taobao", "Facebook")) x = thisset.pop() print(x) #Runoob
然而在交互模式,pop 是删除集合的第一个元素(排序后的集合的第一个元素)。
thisset = set(("Google", "Runoob", "Taobao", "Facebook")) thisset.pop() #'Facebook' print(thisset) #{'Google', 'Taobao', 'Runoob'}
计算集合的元素个数用len
thisset = set(("Google", "Runoob", "Taobao")) len(thisset) #3
clear清空
thisset = set(("Google", "Runoob", "Taobao")) thisset.clear() print(thisset) #set()
判断元素是否存在s中x in s
thisset = set(("Google", "Runoob", "Taobao")) "Runoob" in thisset True "Facebook" in thisset #False >>>
内置方法完整列表参见:
http://www.runoob.com/python3/python3-set.html
- Python中几种数据结构的整理,列表、字典、元组、集合
- python基本数据类型列表、元组、字典、集合
- python元组,字典,列表,set集合
- 每天学一点python---列表、元组、字典、集合
- Python中几种数据结构的整理,列表、字典、元组、集合
- python基础(09)——列表,元组,字典,集合
- Python中列表/元组/集合/字典的理解
- 【python】特色数据类型——元组(列表、元组、字典、集合)(6)
- Python006循环和遍历列表元组集合字典
- Python基础 -- 02 基础语法(字符串、列表、元组、字典、set集合)
- Python黑帽编程2.3 字符串、列表、元组、字典和集合
- Python 列表 元组 字典 集合 区别(一)
- 第七节:python列表、元组、字典、集合
- Python基础二——列表,元组,字典,集合
- Python数据结构(列表、字典、集合、元组)详细解析
- python基础-字符串、列表、元组、字典、集合
- Python(5-6-7):数字+字符串+列表+元组+字典+集合
- python 列表,元组,字典,集合简单总结
- Day2 Python基础学习——字符串、列表、元组、字典、集合
- Python学习笔记ucas(lecture2)数据结构(字符串、列表、元组、字典、集合)