目录
一 列表(list)
1.作用
按位置存放多个值
2.定义
l = [任意类型的值]
3.类型转化
但凡能够被for循环遍历的类型都可以被转化成列表
print(res = list ("hello"))
>>>["h","e","l","l","o"]
4.内置方法
4.1按索引取值
x = ["aa","vv","bb"]
print(x[1])#正向取值
print(x[-1])#反向取值
x[1]="b"#可以索引修改
print(x[1])
>>>vv
>>>bb
>>>b
4.2切片
x = ["aa","vv","bb"]
print(x[0:1])#顾头不顾尾,步长不写为1
>>>["aa"]
4.3长度
x = ["aa","vv","bb"]
print(len(x))
>>>3
4.4成员运算
x = ["aa","vv","bb"]
print("aa" in x)
>>>True
4.5往列表中加值
4.5.1追加
x = ["aa","vv","bb"]
x.append("append")#在最后一个值后面追加
print(x)
>>>['aa', 'vv', 'bb', 'append']
4.5.2追加列表
x = ["aa","vv","bb"]
x.extend([1,2,3,4])#在最后一个值后面追加多个值
print(x)
>>>['aa', 'vv', 'bb', 'append', 1, 2, 3, 4]
4.5.3插入
x = ["aa","vv","bb"]
x.insert(0,"insert")#在指定索引前加值
print(x)
>>>['insert', 'aa', 'vv', 'bb']
4.6删除
4.6.1del
#del是常规删除,没有返回值
x = ["aa","vv","bb"]
del x[0]#后面跟的x[0]相当于索引,也可以切片删除多个值
print(x)
>>>[vv","bb"]
4.6.2pop
#pop有返回值,返回被删除的值
x = ["aa","vv","bb"]
x.pop(0)#括号内是要删除的元素所在索引,不写默认删除最后一个
print(x)
>>>["vv","bb"]
4.6.3remove
#remove返回值为None
x = ["aa","vv","bb"]
l.remove("aa")#指名道姓删除列表中的元素
4.7需要了解的操作
#1.count 统计指定字符出现的个数
x = ["az","av","bb","bb"]
print(x.count("bb"))
>>>2
#2.index 寻找指定字符的索引位置
x = ["az","av","bb","bb"]
print(x.index("bb"))
>>>2
#3.clear 清空列表
x = ["az","av","bb"]
x.clear()
print(x)
>>>[]
#4.reverse 反转列表
x = ["aa","vv","bb"]
x.reverse()
print(x)
>>>['bb', 'vv', 'aa']
#5.sort 排序
x = ["az","av","bb"]
x.sort()#可以对字符串排序,如果是多个字符串,一个一个比较
print(x)
>>>['av', 'az', 'bb']
a = [11,22,33,11]
a.sort()
print(a)
>>>[11, 11, 22, 33]
a.sort(reverse=True)#默认reverse为False,是从小到大,修改reverse可以变成正的
print(a)
>>>[33,22,11,11]
#6 列表的比较
x = ["az","av","bb","bb"] #从第一个元素开始比较
a = ["z"]
print(x>a)
>>>False
二、元组(tupe)
1.作用
元组可以看成一个不可修改的列表,里面可以存放各种类型的值
2.定义
# 在()内用逗号分隔开多个任意类型的值
>>> countries = ("中国","美国","英国") # 本质:countries = tuple("中国","美国","英国")
# 强调:如果元组内只有一个值,则必须加一个逗号,否则()就只是包含的意思而非定义元组
>>> countries = ("中国",) # 本质:countries = tuple("中国")
3.类型转换
只要能被for循环使用的数据类型都可以转换成元组
print(tuple("hello"))
>>>("h","e","l","l","o")
4.内置方法
4.1索引取值
a = (1,2,3)
print(a[1])#只能取,不能改
>>>2
4.2切片
a = ("aa","bb","cc","dd")
print(a[0:2])#顾头不顾尾,步长任意
>>>('aa', 'bb')
4.3长度
a = ("aa","bb","cc","dd")
print(len(a))
>>>4
4.4成员运算
a = ("aa","bb","cc","dd")
print("aa" in a)
>>>True
4.5统计
a = ("aa","aa","bb","cc","dd")
print(a.count("aa"))
>>>2
三、字典(dict)
1定义
# 定义:在{}内用逗号分隔开多元素,每一个元素都是key:value的形式,其中value可以是任意类型,而key则必须是不可变类型,详见第八小节,通常key应该是str类型,因为str类型会对value有描述性的功能
info={'name':'hz','age':18,'sex':'male'} #本质info=dict({....})
# 也可以这么定义字典
info=dict(name='hz',age=18,sex='male')
2类型转换
print(dict([["name","age"],("hz","18")]))
>>>{'name': 'age', 'hz': '18'}
print({}.fromkeys(("name","age")))#只指定key,会直接添加value=None
{'name': None, 'age': None}
3内置操作
3.1 按key存取值
3.1.1取
a = {
"name":"hz",
"age":18,
"hobbies":"play"
}
print(a["name"])
>>>hz
3.1.2 存
#对于赋值操作,如果key原先不存在于字典,则会新增key:value
a = {
"name":"hz",
"age":18,
"hobbies":"play"
}
a["tall"] = 1.81
print(a)
>>>{'name': 'hz', 'age': 18, 'hobbies': 'play', 'tall': 1.81}
#如果key存在则修改value
a["age"] = 20
print(a)
>>>{'name': 'hz', 'age': 20, 'hobbies': 'play', 'tall': 1.81}
3.2长度,成员运算
#长度len(),计算的是key的数量
a = {
"name":"hz",
"age":18,
"hobbies":"play"
}
print(len(a))
>>>3
#成员运算计算的是否存在对应的key值
a = {
"name":"hz",
"age":18,
"hobbies":"play"
}
print("name" in a)
>>>True
3.3删除
#删除对应的键值对
a = {
"name":"hz",
"age":18,
"hobbies":"play"
}
a.pop("name")
print(a)
>>>{'age': 18, 'hobbies': 'play'}
3.4keys,values,items
a = {
"name":"hz",
"age":18,
"hobbies":["play1","play2"]
}
print(a.keys())#获取字典中所有的key
>>>dict_keys(['name', 'age', 'hobbies'])
print(a.values())#获取字典中所有的value
>>>dict_values(['hz', 18, ['play1', 'play2']])
print(a.items())#获取字典中所有的键值对,键值对是元组
>>>dict_items([('name', 'hz'), ('age', 18), ('hobbies', ['play1', 'play2'])])
3.5循环
a = {
"name":"hz",
"age":18,
"hobbies":["play1","play2"]
}
for keys in a:#循环输出key
print(keys)
>>> name
age
hobbies
for values in a.values():#循环输出value
print(values)
>>> hz
18
['play1', 'play2']
for items in a.items():#循环输出键值对
print(items)
>>> ('name', 'hz')
('age', 18)
('hobbies', ['play1', 'play2'])
4需要掌握的操作
#1.get() 查找字典中的值,建议使用
a = {
"name":"hz",
"age":18,
"hobbies":["play1","play2"]
}
print(a.get("name"))#如果key存在,得到对应的value
>>>hz
res = a.get("tall",666)#如果key不存在,默认返回None,也可以修改返回值
print(res)
>>>666
#2.pop() 常规删除操作
a = {
"name":"hz",
"age":18,
"hobbies":["play1","play2"]
}
print(a.pop("name"))#返回值为删除的key对应的value
>>>hz
print(a)
>>>{'age': 18, 'hobbies': ['play1', 'play2']}
#3.popitem() 随机删除操作
a = {
"name":"hz",
"age":18,
"hobbies":["play1","play2"]
}
print(a.popitem())
>>>('hobbies', ['play1', 'play2'])#被删除的值以元组的形式返回
print(a)
>>>{'name': 'hz', 'age': 18}
#4.update() 更新,如果存在则修改,如果不存在则添加新值
a = {
"name":"hz",
"age":18,
"hobbies":["play1","play2"]
}
a.update({"name":"lxt","sex":"flame"})
print(a)
>>>{'name': 'lxt', 'age': 18, 'hobbies': ['play1', 'play2'], 'sex': 'flame'}
#5.setdefault()新增
a = {
"name":"hz",
"age":18,
"hobbies":["play1","play2"]
}
res =a.setdefault("sex","flame")#如果key不存在则新增,并且返回新增的value
print(res)
>>>flame
print(a)
>>>{'name': 'hz', 'age': 18, 'hobbies': ['play1', 'play2'], 'sex': 'flame'}
res =a.setdefault("name","lxt")#如果key存在则返回原来的value值,不影响原字典
print(res)
>>>hz
print(a)
>>>{'name': 'hz', 'age': 18, 'hobbies': ['play1', 'play2']}