容器:存放多个元素的数据类型
列表
集合
元组
字典
列表
定义方式:
1.基于弱数据类型语言的定义 ls = [ 元素1,元素2,元素3... ]
2.通过全局函数list()定义 ls = list ( [ 元素1,元素2,元素3... ] )
#基于弱数据类型语言的定义 >>> ls = [1,2,"a"] >>> ls [1, 2, 'a'] #通过全局函数定义 >>> ls1 = list([3,4,"as"]) >>> ls1 [3, 4, 'as']
常用方法:
1.添加元素
append()--------向列表的尾部添加元素
insert( index , object )------向指定的下标处添加元素 index表示下标,object表示对象
#append()方法 >>> ls = [1,2,"a"] >>> ls.append(3) >>> ls [1, 2, 'a', 3] #insert()方法 >>> ls = [1,2,"a"] >>> ls.insert(0,"ss") >>> ls ['ss', 1, 2, 'a']
2.删除元素
pop()-------删除列表尾部元素,并返回这个元素
remove()------通过元素去删除某个元素
clear()--------清除列表里所有元素
#pop()方法 #用法1 >>> ls = [1,2,3,4,"aa"] >>> ls.pop() #删除列表尾部元素,并返回元素值 'aa' #用法2 >>> ls [1, 2, 3, 4] >>> ls.pop(2) #pop(i) i指的是下标,删除对应下标的元素,并返回元素值 3 >>> ls.pop(10) #当i的取值超出列表下标取值范围,会报错 Traceback (most recent call last): File "", line 1, in IndexError: pop index out of range #remove()方法 >>> ls = [1,2,3,4,"aa"] >>> ls.remove(4) #remove(i) i指的是元素的值 >>> ls [1, 2, 3, 'aa'] #在列表中,某个元素重复出现,删除的是第一次出现的元素 >>> ls = [1,2,3,2,4] >>> ls.remove(2) >>> ls [1, 3, 2, 4] #删除不存在的元素会报错 >>> ls.remove(9) Traceback (most recent call last): File " ", line 1, in ValueError: list.remove(x): x not in list #clear()方法 >>> ls = [1,2,3,2,4] >>> ls.clear() >>> ls []
3.对列表进行操作的方法
排序sort( )
翻转reverse( )
合并extend( )
#sort()方法 >>> ls = [4,1,7,2,5] >>> ls.sort() >>> ls [1, 2, 4, 5, 7] >>> ls = ["c","e","J","S"] >>> ls.sort() >>> ls ['J', 'S', 'c', 'e'] #reverse()方法 >>> ls = [2,"f",7,"西西"] >>> ls.reverse() >>> ls ['西西', 7, 'f', 2] #extend()方法 >>> ls1 = [1,3,"a"] >>> ls2 = ["hh","o"] >>> ls1.extend(ls2) >>> ls1 [1, 3, 'a', 'hh', 'o'] >>> ls2 ['hh', 'o']
4.index()--------返回的是元素在列表中的第一个位置
count()-------返回某个元素在列表里的个数
copy()--------属于浅拷贝对象,不等价于” = "
#index()方法 >>> ls = [2,1,4,2,5,2] >>> ls.index(2) 0 >>> ls.index(1) 1 #count()方法 >>> ls = [2,1,4,2,5,2] >>> ls.count(2) 3 #copy()方法 >>> ls = [1,2,3,4,5] >>> ls1 = ls.copy() >>> ls1 [1, 2, 3, 4, 5] >>> ls.remove(1) >>> ls [2, 3, 4, 5] >>> ls1 #对新列表来说无影响 [1, 2, 3, 4, 5] >>>
集合 set
集合是基于哈希表实现的。哈希表是无序的,不能重复的。集合里的元素是唯一的,无序并不是顺序。比方说 s[0]会报错,因为无序。
定义方式:
1.基于弱数据类型语言的定义 s= { 元素1,元素2,元素3... }
2.通过全局函数set()定义 s = set ( { 元素1,元素2,元素3... } )
常用方法:
1.删除元素
remove(i)---------i指的是元素,移除集合中不存在的元素会报错
discard(i)----------i指的是元素,移除集合中不存在的元素不会报错
clear()-------------清除集合里所有元素
pop()--------------删除集合头部元素,并返回这个元素。如果此集合为空,则引发KeyError异常
#remove()方法
>>> s = {1,2,3}
>>> s.remove(1)
>>> s
{2, 3}
>>> s.remove(4)
Traceback (most recent call last):
File "", line 1, in
KeyError: 4
#discard()方法
>>> s = {1,2,3}
>>> s.discard(1)
>>> s
{2, 3}
>>> s.discard(4)
#clear()方法
>>> s = {1,2,3}
>>> s.clear()
>>> s
set()
#pop()方法
>>> s = {1,2,3}
>>> s.pop()
1
>>> s
{2, 3}
>>> s.pop(0) #没有pop(i)这样的用法
Traceback (most recent call last):
File "", line 1, in
TypeError: set.pop() takes no arguments (1 given)
>>> s ={} #如果此集合为空,则引发KeyError异常
>>> s.pop()
Traceback (most recent call last):
File "", line 1, in
TypeError: pop expected at least 1 argument, got 0
2.添加元素------add()
>>> s = {1,2,3,4}
>>> s.add(5)
>>> s
{1, 2, 3, 4, 5}
>>> s.add(1) #添加重复的元素不会报错,但不能成功添加
>>> s
{1, 2, 3, 4, 5}
3.copy() -------将集合进行一次浅拷贝
>>> s = {1,2,3,4,5}
>>> s1 = s.copy()
>>> s1
{1, 2, 3, 4, 5}
>>> s.remove(3)
>>> s
{1, 2, 4, 5}
>>> s1 #对新集合无影响
{1, 2, 3, 4, 5}
4.差集------difference( )
交集------intersection( )
并集------union( )
更新集合-------update()
>>> s = {1,2,3,4,5}
>>> s1 ={2,3,4,5}
>>> s2 ={3,4,5,6}
>>> s.difference(s2) #s相较于s2的差集,以s为基准
{1, 2}
>>> s.intersection(s1)
{2, 3, 4, 5}
>>> s.union(s2)
{1, 2, 3, 4, 5, 6}
>>> s #差集,交集,并集不改变原集合
{1, 2, 3, 4, 5}
>>> s1
{2, 3, 4, 5}
>>> s2
{3, 4, 5, 6}
#update()方法会改变原集合的值
>>> s = {1,2,3}
>>> s1 = {3,4,5}
>>> s.update(s1)
>>> s
{1, 2, 3, 4, 5}
>>> s1
{3, 4, 5}
元组
元组是有序的,可以通过下标获取元素。
元组是不可变类型(即指向不可变),但元组里的元素可以是可变类型。通过下述代码进行说明
>>> t = (1,3,"a",["c","m"]) >>> t[0] 1 >>> t[0]=9 #指向下标0处的元素是不可变的,即元素1不能被换成元素9 Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment >>> t[3][0]="oo" #t[3]指向的列表不可变,但列表里的元素可以更换 >>> t[3][1]="pp" >>> t (1, 3, 'a', ['oo', 'pp'])
定义方式:
1.基于弱数据类型语言的定义 t = ( 元素1,元素2,元素3... )
2.通过全局函数tuple( )定义 t = tuple ( (元素1,元素2,元素3...) )
#1.基于弱数据类型语言的定义 >>> t = (1,2,3,1) >>> t (1, 2, 3, 1) #2.通过全局函数来定义 >>> t = tuple((1,3,5,7)) >>> t (1, 3, 5, 7) #定义只有一个元素的元组 >>> t = (1,) >>> t (1,) >>> type(t)
常用方法:
1.count()-----返回某个元素在元组里的个数
2.index()-----返回的是元素在元组中的第一个位置
>>> t = (1,3,2,5,3,23,8) >>> t.count(3) 2 >>> t.index(3) 1
字典
定义方式:
1.基于弱数据类型语言的定义 d = { "key1":"value1" ,"key2":"value2" .....}
2.通过全局函数dict( )定义 d =dict ( { "key1":"value1" ,"key2":"value2" .....} )
>>> d = {"name":"小马","age":20,"hobby":"睡觉"}
>>> d
{'name': '小马', 'age': 20, 'hobby': '睡觉'}
>>> d = dict({"name":"小宋","age":20,"hobby":"煲剧"})
>>> d
{'name': '小宋', 'age': 20, 'hobby': '煲剧'}
常用方法:
1.删除
clear()-------清空字典
pop( )---------通过key值删除键值对,若key存在返回的是删除的键值对
popitem( )------删除最后一个键值对,返回的是删除的键值对
#clear()方法
>>> d = {"name":"小马","age":20,"hobby":"睡觉"}
>>> d.clear()
>>> d
{}
#pop()方法
>>> d = {"name":"小马","age":20,"hobby":"睡觉"}
>>> d.pop("name")
'小马'
>>> d.pop("sex") #如果key值不存在,抛出异常
Traceback (most recent call last):
File "", line 1, in
KeyError: 'sex'
#popitem()方法
>>> d = {"name":"小马","age":20,"hobby":"睡觉"}
>>> d.popitem()
('hobby', '睡觉')
2.copy()------将字典进行一次浅拷贝,参考上述案例
3.setdefault( )-------设置默认值
>>> d ={"name":"mm","age":20,"sex":"female"}
>>> d.setdefault("hobby")
>>> d
{'name': 'mm', 'age': 20, 'sex': 'female', 'hobby': None}
>>> d.setdefault("name") #若设置的默认值存在,返回对应的value值
'mm'
4.获取
通过key获取value的值------get( )方法
获取所有的键------------------keys( )方法
获取所有的值------------------values( )方法
获取所有的键值对------------items( )方法
>>> d = {"name":"mm","age":10,"sex":"female"}
>>> d.get("name")
'mm'
>>> d.get("habit") #获取不存在的key,返回的是None
>>> d.keys()
dict_keys(['name', 'age', 'sex'])
>>> d.values()
dict_values(['mm', 10, 'female'])
>>> d.items()
dict_items([('name', 'mm'), ('age', 10), ('sex', 'female')])
>>>



