python中有很多容器用来存储数据,方便再次用到,这里介绍列表,集合和字典的常用方法。
文章目录
一.列表(list)
- 列表是可变的,有序的,可重复的
1. 索引
正索引 :从左至右 0 开始
负索引 :从右至左 -1 开始
2. 列表切片
list = [1,2,3,4,5,6,7,8,9]
print(list[2:])
>>> [3, 4, 5, 6, 7, 8, 9]
print(list[2:6])
>>> [3, 4, 5, 6]
3. in
判断元素是否在列表
list = [1,2,3,4,5,6,7,8,9]
3 in list
>>> True
4. 常用方法
index (索引)
list 索引从0开始
list = [1,2,2,3,4,5,6,6,6]
list.index(2)
>>> 1
count (计数)
计算元素出现的次数
list = [1,2,2,3,4,5,6,6,6]
list.count(6) #元素出现次数
>>> 3
len (长度)
列表的长度(或元素个数)
list = [1,2,3,4,5,6]
len(list) # 列表长度
>>> 6
append (尾部追加)
list = [1,2,3,4]
list.append(6)
print(list)
>>> [1, 2, 3, 4, 6]
insert (插入)
list = [1,2,3,4,5,6]
list.insert(2,9) # 在index=2处插入元素9
print(list)
>>> [1, 2, 9, 3, 4, 5, 6]
extend (扩展)
list = [1,2,2,3,4,5,6,6,6]
list.extend(range(100,105))
print(list)
>>> [1, 2, 2, 3, 4, 5, 6, 6, 6, 100, 101, 102, 103, 104]
remove (移除)
移除元素,若元素重复出现则移除第一个出现的元素
list = [1,2,3,4,5,5,6]
list.remove(3) # 删除元素,两个5,则删除第一个
print(list)
>>> [1, 2, 4, 5, 6]
pop (弹出)
list = [1,2,3,4,5,6]
list.pop(3) # pop(index) 删除索引的数
print(list)
>>> [1, 2, 3, 5, 6]
list.pop() # 随机删除
print(list)
>>> [1, 2, 3, 5]
sort (排序)
list = [1,8,6,3,5,'12'] # '12'为str
list.sort(key=int,reverse=False) #把元素转为int,排序,reverse表示是否倒序
print(list)
>>> [1, 3, 5, 6, 8, '12']
copy (浅拷贝)
# 浅拷贝copy
list1 = [1,2,[5,6]] # list1引用了列表[5,6]
#list2只是拷贝了list1中[5,6]列表的储存地址,但没有拷贝列表内存地址指向的值
list2 = list1.copy()
print(list2)
>>> [1, 2, [5, 6]]
# ---------------------------------------------------------------------------
list1[2][1] = 200 # 改变list1中6的值
print(list1)
>>> [1, 2, [5, 200]]
# ---------------------------------------------------------------------------
print(list2) # 6的值也改变,list2只是拷贝了list1中列表的储存地址,储存地址指向同一个值
>>> [1, 2, [5, 200]]
deepcopy (深拷贝)
# 深拷贝copy.deepcopy
import copy
list1 = [1,2,[5,6]] # list1引用了列表[5,6]
list2 = copy.deepcopy(list1) #list2拷贝了list1中[5,6],并储存在新地址
print(list2)
>>> [1, 2, [5, 6]]
# -------------------------------------------------------------------
list1[2][1] = 200 # 改变list1中6的值
print(list1)
>>> [1, 2, [5, 200]]
# -------------------------------------------------------------------
print(list2) # 6的值没有改变
>>> [1, 2, [5, 6]]
二.集合(set)
集合是可变的,无序的,去重
集合的元素必须是可hash的(即不可变的,如列表可变,列表不能为集合中的元素)
1. 常用方法
add(单个添加)
sl = set()
sl.add(5)
print(sl)
>>> {5}
update(多个)
set = {1,6,5,8,3}
set.update(range(102,105)) #批量增加元素
print(set)
>>> {1, 3, 5, 6, 8, 102, 103, 104}
discard (删除)
set = {1,6,5,8,3}
set.discard(3) #删除元素
print(set)
>>> {1, 5, 6, 8}
并 |
set1 = {1,2,3}
set2 = {4,5,6}
set1 | set2
>>> {1, 2, 3, 4, 5, 6}
交 &
set1 = {1,2,3,5,9,7}
set2 = {2, 3, 5, 8}
set1 & set2
>>> {2, 3, 5}
对称差集 ^
set1 = {1,2,3,4,5}
set2 = {2,4,6,8,9}
set1 ^ set2
>>> {1, 3, 5, 6, 8, 9}
三.元组(tuple)
元组是有序的,不可变的,可重复
可切片,索引,但不能增,减,删。
四.字典(dict)
字典是无序的,可变的,字典的键不可重复,值可重,键不可变,值可变
1. 易错点
name = 'tom'
id = 6
dict = {}
dict['name'] = id >>> {'name': 6}
dict[name] = id >>> {'tom': 6}
(1)dict = {‘Tom’:01,‘Jerry’:02}
查找值 dict[‘Tom’] >>>> 01
dict.get(‘Tom’) >>>> 01
2. 常用方法
增加元素
#增加元素一
dict2 = {}
name = 'tom'
age = 18
dict2 = {'name':name,'age':age}
print(dict)
>>> {'name': 'jerry', 'age': 18}
# ---------------------------------------------------------
#增加元素二
dict3 ={}
name='tom'
id = 6
dict3[id]=name # dict[key]=value,!!!区别dict3['id']=name
print(dict3)
>>> {6: 'tom'}
删除元素
#删除元素
dict = {}
id1 = 6
name1 = 'tom'
id2 = 5
name2 = 'peter'
dict[id1]=name1
dict[id2]=name2
print(dict)
>>> {6: 'tom', 5: 'peter'}
del(dict[id2])
print(dict)
>>> {6: 'tom'}
改变元素
# 改变元素
dict1 = {'name':'tom', 'age':18}
dict1['name'] = 'jerry'
print(dict)
>>> {'name': 'jerry', 'age': 18}
3. 遍历字典
遍历键
# 遍历字典中的键
#方法一
age = 16
name = 'tom'
height = 175
dict = {'name':name, 'age':age, 'height':height}
for i in dict.keys(): # keys 不是 key
print(i)
>>>:
name
age
height
# 方法二
age = 16
name = 'tom'
height = 175
dict = {'name':name, 'age':age, 'height':height}
for i in dict: # 遍历字典时,字典会把键传给i
print(i)
>>>:
name
age
height
遍历值
# 遍历字典中的值
#方法一
age = 16
name = 'tom'
height = 175
dict = {'name':name, 'age':age, 'height':height}
for i in dict.values(): # values 不是 value
print(i)
>>>:
tom
16
175
#方法二
list = []
age = 16
name = 'tom'
height = 175
dict = {'name':name, 'age':age, 'height':height}
list.append(dict)
for i in list:
print(i['name'],i['age'],i['height'])
>>> tom 16 175
# 方法三
age = 16
name = 'tom'
height = 175
dict = {'name':name, 'age':age, 'height':height}
for i in dict: # 遍历字典时,字典会把键传给i
print(dict[i])
>>>:
tom
16
175