第四节 列表 和字典
什么是列表:
- 数据类型:可以存储多个数据的数据类型。
#5个人 5个大佬 ,使用列表
big_boss = ["Demons","Go","EE","上善若水","Summer"]
print(big_boss)
结果:
['Demons', 'Go', 'EE', '上善若水', 'Summer']
列表的表示方法:
- 表示列表 “mystring”[1] 不是列表,切片的表示
- 区别 :mtr[]
- 列表当中每个元素可以是python任意数据类型
- array vs list :array 只能同一种数据类型
#元素可以是任意的数据类型
big_boss =["Demons",11,"EE","上善若水","Summer"]
print(big_boss)
big_boss =["Demons",11,True,33.33,["a","b",["zhengzi",True]]]
print(big_boss)
打印结果
['Demons', 11, 'EE', '上善若水', 'Summer']
['Demons', 11, True, 33.33, ['a', 'b',['zhengzi', True]]]
索引:获取某一个元素
big_boss =["Demons",11,True,33.33,["a","b",["zhengzi",True]]]
print(big_boss[0])
print(big_boss[-1])
print(big_boss[-2])
打印结果
Demons
['a', 'b', ['zhengzi', True]]
33.33
切片:获取多个元素
big_boss =["Demons",11,True,33.33,["a","b",["chengzi",True]]]
# 切片
print(big_boss[:3])
#获取 a
print(big_boss[-1][0])
last_one = big_boss[-1]#["a","b",["zhengzi",True]]
print(last_one[0])
#获取chengzi
print(big_boss[-1][-1][0])
打印结果
['Demons', 11, True]
a
a
chengzi
嵌套列表的元素获取
lst = [
"a",
"b",
[1,2,3]
]
print(lst[0])
#[1,2,3]
print(lst[-1][1])
打印结果
a
2
列表和字符串的区别
-
列表有其他的操作 增删改查
-
字符串只能查
列表的添加操作
append ,是添加到最后
增加某个元素,修改的是原来的变量 lst
lst=["yuz","shangshan","rita"]
lst.append("裴纶")
print(lst)
打印
['yuz', 'shangshan', 'rita', '裴纶']
-insert (索引,data)
lst=["yuz","shangshan","rita"]
lst.append("裴纶")
print(lst)
lst.insert(1,"仙人球")
print(lst)
打印
['yuz', 'shangshan', 'rita', '裴纶']
['yuz', '仙人球', 'shangshan', 'rita', '裴纶']
添加多个元素
#合并两个列表,添加多个
lst=["yuz","shangshan","rita"]
lst.extend(["tiger","f2"])
print(lst)
打印
['yuz', 'shangshan', 'rita', 'tiger', 'f2']
lst=["yuz","shangshan","rita"]
#append,extend
lst = [1,2,3]
lst.append(3)
print(f"第一次尝试{lst}")
lst.extend(3)
print(f"第二次尝试{lst}")
打印
Traceback (most recent call last):
File "E:/ltt/02pythonProjet/pythonProject1/deom4.py", line 9, in <module>
lst.extend(3)
TypeError: 'int' object is not iterable
第一次尝试[1, 2, 3, 3]
lst=["yuz","shangshan","rita"]
#append,extend
lst = [1,2,3]
lst.append(3)
print(f"第一次尝试{lst}")
lst.extend([3])
print(f"第二次尝试{lst}")
打印
第一次尝试[1, 2, 3, 3]
第二次尝试[1, 2, 3, 3, 3]
a=[1,2,3]
b=[4,5,6]
#b 是作为一个整体,一个元素,添加到 a 当中
a.append(b)
print(a)
打印
[1, 2, 3, [4, 5, 6]]
a=[1,2,3]
b=[4,5,6]
# b 是拆开里面的元素,作为多元素,添加到a当中
a.extend(b)
print(a)
打印
[1, 2, 3, 4, 5, 6]
列表的删除
-
-remove
-
-delete
-
-pop
-
#remove:在列表当中删除指定的值
big_boss = ["糖","木易","君君"]
big_boss.remove("木易")
print(big_boss)
打印
['糖', '君君']
big_boss = ["糖","木易","君君"]
big_boss.remove("木易")
print(big_boss)
big_boss.append("木易")
print(big_boss)
结果
['糖', '君君']
['糖', '君君', '木易']
- #delete 异类 :尽量不要用
big_boss = ["糖","木易","君君"]
# big_boss.remove("木易")
# print(big_boss)
# big_boss.append("木易")
# print(big_boss)
#delete
del big_boss[0]
print(big_boss)
结果:
['木易', '君君']
- #pop 0 代表所有为 0
big_boss = ["糖","木易","君君"]
big_boss.pop(0)
#获取索引为 0 的值
big_boss[0]
print(big_boss)
结果
['木易', '君君']
列表的修改
lst =[1,2,3]
lst[1] ="a"
print(lst)
结果:
[1, ‘a’, 3]
lst =[1,2,3]
lst[1],lst[2] ="c","d"
print(lst)
结果
[1, ‘c’, ‘d’]
lst =[1,2,3]
lst[1:]=5,6
print(lst)
结果:
[1, 5, 6]
列表的方法
- -index
- -count
- -sort
- -reverse
- -clear
lst =[4,5,6,5]
#统计
print(lst.count(5))
结果:
2
lst =[4,5,6,5]
#index 查找的是第一次出现的索引位置
print(lst.index(5))
结果:
1
lst =[4,5,6,5,[4,5]]
#排序
print(lst.sort())
结果:
Traceback (most recent call last):
File “E:/ltt/02pythonProjet/pythonProject1/demo9.py”, line 7, in
print(lst.sort())
TypeError: ‘<’ not supported between instances of ‘list’ and ‘int’
lst =[4,5,6,5]
print(lst.sort())
print(lst)
结果:
None
[4, 5, 5, 6]
lst =[4,5,6,5]
lst.reverse()
print(lst)
结果:
[5, 6, 5, 4]
lst =[4,5,6,5]
lst.sort()
#反向排序、逆序,相当于[::-1]
lst.reverse()
print(lst)
结果:
[6, 5, 5, 4]
lst =[4,5,6,5]
#反向排序
lst.sort(reverse=True)
print(lst)
结果:
[6, 5, 5, 4]
lst =[4,5,6,5]
#清除一个列表
lst.clear()
print(lst)
结果:
[ ]
字典
-
也是可以进行增删改查的
-
也是存储多个数据的
-
字典能够表示元素更具体的意思,每个元素表示的意义是什么,可以通过key命名
-
key是有要求的:
-
key 不能出现重名,在一个字典当中,key之间是不一样的
#重名的 key beisheng={"favor":"星际穿越", "hate":"蜘蛛侠", "first":"上海堡垒", "favor":"分手大师", "twice":"前任3"} #favor 后面的值会覆盖前面的值 print(beisheng["favor"]) print(beisheng)
结果:
分手大师
{‘favor’: ‘分手大师’, ‘hate’: ‘蜘蛛侠’, ‘first’: ‘上海堡垒’, ‘twice’: ‘前任3’} -
key 是不能变的。列表是不能作为key的
-
#key 不能是列表
beisheng={"favor":"星际穿越",
["hate"]:"蜘蛛侠",
"first":"上海堡垒",
"last":"分手大师",
"twice":"前任3"}
结果:
Traceback (most recent call last):
File “E:/ltt/02pythonProjet/pythonProject1/demo10.py”, line 21, in
beisheng={“favor”:“星际穿越”,
TypeError: unhashable type: ‘list’
- 列表,当每个元素具有意义,你又想去单独获取的时候,可读性不强
- 字典:key :元素的名称 。value:元素的值。键值对:成对
- 用{ } 在最外面
- key:value , key2 :value1,
beisheng=["星际穿越","蜘蛛侠","上海堡垒","分手大师","前任3"]
print(beisheng[2])
beisheng={"favor":"星际穿越","hate":"蜘蛛侠","first":"上海堡垒","last":"分手大师","twice":"前任3"}
print(beisheng["hate"])
结果:
上海堡垒
蜘蛛侠
字典增删改查操作
获取 、查
beisheng={"favor":"星际穿越",
"hate":"蜘蛛侠",
"first":"上海堡垒",
"last":"分手大师",
"twice":"前任3"}
#获取 、查
print(beisheng["hate"])
结果:蜘蛛侠
- 字典没有索引和切片
修改
beisheng={"favor":"星际穿越",
"hate":"蜘蛛侠",
"first":"上海堡垒",
"last":"分手大师",
"twice":"前任3"}
#修改
beisheng["hate"] = "蝙蝠侠"
print(beisheng)
结果:{‘favor’: ‘星际穿越’, ‘hate’: ‘蝙蝠侠’, ‘first’: ‘上海堡垒’, ‘last’: ‘分手大师’, ‘twice’: ‘前任3’}
添加
beisheng={"favor":"星际穿越",
"hate":"蜘蛛侠",
"first":"上海堡垒",
"last":"分手大师",
"twice":"前任3"}
#添加 和 修改是一样的
#什么时候是修改,什么时候又是添加
#看 key :当 key 已经存在的时候,修改,当之前没有这个key,就是添加
beisheng["scared"] = "贞子"
print(beisheng)
beisheng["scared"] = "午夜凶铃"
print(beisheng)
结果:
{‘favor’: ‘星际穿越’, ‘hate’: ‘蜘蛛侠’, ‘first’: ‘上海堡垒’, ‘last’: ‘分手大师’, ‘twice’: ‘前任3’, ‘scared’: ‘贞子’}
{‘favor’: ‘星际穿越’, ‘hate’: ‘蜘蛛侠’, ‘first’: ‘上海堡垒’, ‘last’: ‘分手大师’, ‘twice’: ‘前任3’, ‘scared’: ‘午夜凶铃’}
删除
beisheng={"favor":"星际穿越",
"hate":"蜘蛛侠",
"first":"上海堡垒",
"last":"分手大师",
"twice":"前任3",
"scared":"午夜凶铃"}
#删除
#列表 remove , pop
beisheng.pop("scared")
print(beisheng)
结果:
{‘favor’: ‘星际穿越’, ‘hate’: ‘蜘蛛侠’, ‘first’: ‘上海堡垒’, ‘last’: ‘分手大师’, ‘twice’: ‘前任3’}
beisheng={"favor":"星际穿越",
"hate":"蜘蛛侠",
"first":"上海堡垒",
"last":"分手大师",
"twice":"前任3",
"scared":"午夜凶铃"}
#随机删除
beisheng.popitem()
print(beisheng)
结果:{‘favor’: ‘星际穿越’, ‘hate’: ‘蜘蛛侠’, ‘first’: ‘上海堡垒’, ‘last’: ‘分手大师’, ‘twice’: ‘前任3’}
查
beisheng={"favor":"星际穿越",
"hate":"蜘蛛侠",
"first":"上海堡垒",
"last":"分手大师",
"twice":"前任3",
"scared":"午夜凶铃"}
#查
print(beisheng.keys())
print(beisheng.values())
print(beisheng.items())
结果:
dict_keys([‘favor’, ‘hate’, ‘first’, ‘last’, ‘twice’, ‘scared’])
dict_values([‘星际穿越’, ‘蜘蛛侠’, ‘上海堡垒’, ‘分手大师’, ‘前任3’, ‘午夜凶铃’])
dict_items([(‘favor’, ‘星际穿越’), (‘hate’, ‘蜘蛛侠’), (‘first’, ‘上海堡垒’), (‘last’, ‘分手大师’), (‘twice’, ‘前任3’), (‘scared’, ‘午夜凶铃’)])
元组
- 元组和列表非常相似
- 元组是不可变的只能获取(索引、切片),列表是可变的(增加,修改,删除)
- 元组表示:( ) , 列表 [ ]
tp = (3,4,5)
print(tp)
print(type(tp))
结果:
(3, 4, 5)
<class ‘tuple’>
- 空元祖
#空元组
tp =()
print(tp)
结果:()
- 一个元素的元组,在元素的后面一定要加逗号 (1,)
#一个元素的元组,在元素的后面一定要加逗号 (1,)
#(1)
tp = (1)
print(tp)
print(type(tp))
tp = (1,)
print(tp)
print(type(tp))
结果:
1
<class ‘int’>
(1,)
<class ‘tuple’>
tp = (3,4,5)
tp[1] =0
print(tp)
结果:
Traceback (most recent call last):
File “E:/ltt/02pythonProjet/pythonProject1/demo12.py”, line 3, in
tp[1] =0
TypeError: ‘tuple’ object does not support item assignment
查找
tp = (3,4,5)
#查找
print(tp[0])
结果:3
集合 set
s = {1,2,3}
print(s)
结果:{1, 2, 3}
- 集合当中元素不能重复
s = {1,2,3,1,2,3,1,1,1,1,4}
print(s)
结果:{1, 2, 3, 4}
- 典型的应用场景:去重
#使用列表存储用例
money =[1,5,100,0,1,100]
print(len(money))
#转化成集合
money_after = list(set(money))
print(len(money_after))
结果:
6
4
数据类型的总结
-string
-int
-float
-bool
-list
-dict
-tuple
-set
-None 表示什么都没有
bool
- 只要是代表 0 ,空就是表示False
- 否则就是 True
a ="4"
b =bool(a)#true
b =bool([1])#true
b =bool({"name":"chengzi"})#true
b = bool(7)#true
print(b)#true
#false
#对数字进行bool 转化 ,非0 为true, 0为false
f = bool(0)#false
f = bool(-1000)#true
#字符串 空字符串为 false ,非空的True
f = bool("") #False
f = bool(" ")#空格为True
print(f)
#list
l = bool([])#False
None
#list
f = bool([])
f =bool(None)
print(f)
结果:False
不可变的数据类型
- 字符串不可变
- int
- string
- int
- float
- bool
- tuple
可变的
- list
- dict
- set
#字符串为什么是不可变的数据类型
name = "Double King"
lastname = "wang"
# name+lastname
print(name)
print(lastname)
#生成了一个新的字符串
full_name = name+lastname
print(full_name)
结果:
Double King
wang
Double Kingwang
#类型转化
a = 4
b =str(a)
print(a)
print(b)
结果:
4
4
#列表是可变的吗?
lst = [1,2,3]
lst.append(4)
print(lst)
结果:
[1, 2, 3, 4]
a = [1,2,3,("a","b")]
#a[-1] 修改的是一个列表还是元组 a
a[-1] =["a","b"]
print(a)
a = (1,2,["a","b"])
#a[-1] ="c"
# print(a)
print(type(a[-1]))
b = a[-1]
b[-1] = "c"
a[-1][-1]="c"
print(a)
结果:
[1, 2, 3, [‘a’, ‘b’]]
<class ‘list’>
(1, 2, [‘a’, ‘c’])