一、列表list
a.基础
1.
li = [1, 12, 9, "age", ["石振文", ["19", 10], "庞麦郎"], "alex", True]
中括号括起来;,分割每个元素;列表中的元素可以是数字,字符串,列表,布尔值......;列表可以嵌套
(他就是个集合,内部放置任何东西)
2.可以进行索引,切片取值
li = [1, 12, 9, "age", ["石振文", ["19", 10], "庞麦郎"], "alex", True]
print(li[2])
print(li[2:5])
3.利用for、while循环 continue和break也可以用
4.不同于字符串,列表元素是可以被修改的 索引、切片也可以修改
li = [1, 12, 9, "age", ["石振文", ["19", 10], "庞麦郎"], "alex", True]
li[2] = 1200
print(li)
li = [1, 12, 9, "age", ["石振文", ["19", 10], "庞麦郎"], "alex", True]
li[2:3] = [90, 100]
print(li)
5.删除 del 列表[a] /del 列表[a:b]
li = [1, 12, 9, "age", ["石振文", ["19", 10], "庞麦郎"], "alex", True]
del li[2:6]
print(li)
6.支持in操作 in 、not in 进行列表判断
li = [1, 12, 9, "age", ["石振文", ["19", 10], "庞麦郎"], "alex", True]
v=12 in li
print(v)
7.可以一直往里边找
li = [1, 12, 9, "age", ["石振文", ["19", 10], "庞麦郎"], "alex", True]
print(li[4][2][0])
8.字符串直接转成list格式,他默认每一个字符成为列表元素(内部使用for循环)
数字不能迭代(循环),不能转换为list类型
9.列表转换为字符串的时候,需要自己调用for循环,否则系统直接加一个" " 既有数字又有字符串,用下面的方法
li = [11, 22, 33, 44, "123", "alex"]
s = ""
for i in li:
s = s + str(i)
print(s)
列表中的元素如果只有字符串的话,用join
li = [ "123", "alex"]
v="".join(li)
print(v)
b.list类中提供的方法
1.append 原来值最后追加
li = [11, 22, 33, 22, 44]
v=li.append([1234,2323])
print(li)
print(v)
2.clear 清空列表
li = [11, 22, 33, 22, 44]
li.clear()
print(li)
3.拷贝,浅拷贝cope
v = li.copy()
print(v)
4.count 计算元素出现的次数
li = [11, 22, 33, 22, 44]
v = li.count(22)
print(v)
5.extend 扩展原列表,参数:可迭代对象 区分一下extend appendli = [11, 22, 33, 22, 44]li.append([9898,"不得了"])
[11, 22, 33, 22, 44, [9898, '不得了']]
li.extend([9898,"不得了"])
for i in [9898,"不得了"]:
li.append(i)
[11, 22, 33, 22, 44, 9898, '不得了']
li.extend("不得了")
print(li)
[11,22,33,44,9898,“不”,"得”,"了"]
6.index 根据值获取当前值索引位置(左边优先)
li = [11, 22, 33, 22, 44]
v= li.index(22)
print(v)
7. insert 在指定索引位置插入元素
li = [11, 22, 33, 22, 44]
li.insert(0,99)
print(li)
[]
8、pop 删除某个值(1.指定索引;2. 默认最后一个),并获取删除的值
li = [11, 22, 33, 22, 44]
v = li.pop()
print(li)
print(v)
li = [11, 22, 33, 22, 44]
v = li.pop(1)
print(li)
print(v)
9.remove 删除列表中的指定值,左边优先
li = [11, 22, 33, 22, 44]
li.remove(22)
print(li)
PS:删除 pop remove del li[0] del li[7:9] clear
10. reverse 将当前列表进行翻转
li = [11, 22, 33, 22, 44]
li.reverse()
print(li)
11. sort 列表的排序
li = [11,44, 22, 33, 22]
li.sort() 从小到大排序
li.sort(reverse=True) 从大到小排序
print(li)
二、元祖
元组,元素不可被修改,不能被增加或者删除
tuple
tu = (11,22,33,44)
tu.count(22),获取指定元素在元组中出现的次数
tu.index(22)
1. 书写格式
tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
一般写元组的时候,推荐在最后加入 ,
元素不可被修改,不能被增加或者删除
2. 索引
v = tu[0]
print(v)
3. 切片
v = tu[0:2]
print(v)
4. 可以被for循环,元祖可迭代对象
for item in tu:
print(item)
5. 转换
s = "asdfasdf0"
li = ["asdf","asdfasdf"]
tu = ("asdf","asdf")
v = tuple(s)
print(v)
v = tuple(li)
print(v)
v = list(tu)
print(v)
v = "_".join(tu)
print(v)
li = ["asdf","asdfasdf"]
li.extend((11,22,33,))
print(li)
6.元组的一级元素不可修改/删除/增加
tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
元组,有序。
v = tu[3][0][0]
print(v)
v=tu[3]
print(v)
tu[3]=123 报错:元祖的一级元素不能修改
tu[3][0] = 567 进入了列表,列表内部可以修改
print(tu)
7.tu.count(2),获取指定元素在元祖中出现的次数
tu.index(22),获取某个值的索引位置
三、字典
dict
dict 键值对 字典的key:数字,字符串,元祖 字典的value可以是任意类型的值 字典是无序的
基本结构
dic = {
"k1": 'v1',
"k2": 'v2'
} 两个键值对
1 根据序列,创建字典,并指定统一的值
v = dict.fromkeys(["k1",123,"999"],123)
print(v)
v=v[key] 可以取里边的值
2 根据Key获取值,key不存在时,可以指定默认值(None)
v = dic['k11111']
print(v)
v = dic.get('k1',111111)
print(v)
3 删除del v[key][index]
删除并获取值
dic = {
"k1": 'v1',
"k2": 'v2'
}
v = dic.pop('k1',90)
print(dic,v)
k,v = dic.popitem()
print(dic,k,v)
4 设置值,
已存在,不设置,获取当前key对应的值
不存在,设置,获取当前key对应的值
dic = {
"k1": 'v1',
"k2": 'v2'
}
v = dic.setdefault('k1111','123')
print(dic,v)
5 更新
dic = {
"k1": 'v1',
"k2": 'v2'
}
dic.update({'k1': '111111','k3': 123})
print(dic)
dic.update(k1=123,k3=345,k5="asdf")
print(dic)
6 keys() 7 values() 8 items() get update
#########
1、基本机构
info = {
"k1": "v1", # 键值对
"k2": "v2"
}
### 2 字典的value可以是任何值
info = {
"k1": 18,
"k2": True,
"k3": [
11,
[],
(),
22,
33,
{
'kk1': 'vv1',
'kk2': 'vv2',
'kk3': (11,22),
}
],
"k4": (11,22,33,44)
}
print(info)
### 3 列表、字典不能作为字典的key
info ={
1: 'asdf',
"k1": 'asdf',
True: "123",
# [11,22]: 123
(11,22): 123,
# {'k1':'v1'}: 123
}
print(info)
4 字典无序
info = {
"k1": 18,
"k2": True,
"k3": [
11,
[],
(),
22,
33,
{
'kk1': 'vv1',
'kk2': 'vv2',
'kk3': (11,22),
}
],
"k4": (11,22,33,44)
}
print(info)
5、索引方式找到指定元素
info = {
"k1": 18,
2: True,
"k3": [
11,
[],
(),
22,
33,
{
'kk1': 'vv1',
'kk2': 'vv2',
'kk3': (11,22),
}
],
"k4": (11,22,33,44)
}
# v = info['k1']
# print(v)
# v = info[2]
# print(v)
v = info['k3'][5]['kk3'][0]
print(v)
6 字典支持 del 删除
info = {
"k1": 18,
2: True,
"k3": [
11,
[],
(),
22,
33,
{
'kk1': 'vv1',
'kk2': 'vv2',
'kk3': (11,22),
}
],
"k4": (11,22,33,44)
}
del info['k1']
del info['k3'][5]['kk1']
print(info)
7 for循环
dict
info = {
"k1": 18,
2: True,
"k3": [
11,
[],
(),
22,
33,
{
'kk1': 'vv1',
'kk2': 'vv2',
'kk3': (11,22),
}
],
"k4": (11,22,33,44)
}
对字典for循环的时候,默认循环它的所有的key
for item in info:
print(item)
for item in info.keys():
print(item)
for item in info.values():
print(item)
for item in info.keys():
print(item,info[item])
for k,v in info.items(): (items获取键值对)
print(k,v)
True 1 False 0
info ={
"k1": 'asdf',
True: "123",
# [11,22]: 123
(11,22): 123,
# {'k1':' v1'}: 123
}
print(info)
四、小结
一、数字
int(..)
二、字符串
replace/find/join/strip/startswith/split/upper/lower/format
tempalte = "i am {name}, age : {age}"
# v = tempalte.format(name='alex',age=19)
v = tempalte.format(**{"name": 'alex','age': 19})
print(v)
三、列表
append、extend、insert
索引、切片、循环
四、元组
忽略
索引、切片、循环 一级元素不能被修改
五、字典
get/update/keys/values/items
for,索引
dic = {
"k1": 'v1'
}
v = "k1" in dic
print(v)
v = "v1" in dic.values()
print(v)
六、布尔值
0 1
bool(...)
None "" () [] {} 0 ==> False