矩阵:
xy = [[10,20,30,40],
[100,200,300,400],
["A","B","C","D"]]
print(xy) #结果为[[10, 20, 30, 40], [100, 200, 300, 400], ['A', 'B', 'C', 'D']]
print(xy[1][2]) #结果为300
元组:(与列表几乎一样)
#创建、编辑。遍历
xt = tuple()
yt = (4,10,23,20)
print(yt[3]) #元组里的数据不能改变,所以访问速度很快
yt.index() #获得元素的下标
字典:
#创建
dictx = dict()
dictx = {}
dicty = {"2019001":"han","2019002":"shuqi","2019003":"feng"}
#根据已有数据创建
#字典无序,不能用下标
print(disty["2019003"]) #访问
#添加、修改、删除
dicty["2019003"] = "ping" #修改
print(dicty["2019003"])
dicty["2019004"] = "heru" #添加,里面没有这个key,自动添加相应数据
a = dicty.pop("2019002")
print(dicty)
print(a) #结果为"shuqi"
#遍历
for i in dicty.keys():
print(i)
print(dicty.keys()) #dict_keys(['2019001', '2019002', '2019003'])
print(list(dicty.keys())) #['2019001', '2019002', '2019003']
for i in dicty: #效果与for i in dicty.keys():相同
print(dicty[i]) #效果与for i in dicty.values():相同
for i in dicty.values():
print(i)
for i in dicty.items():
print(i)
for t,s in dicty.items(): #将字典里的key和value分别赋值为t,s
print(t)
#当key值不存在时:
print(dicty.get("2019005","没有这个学生!")) #结果为没有这个学生!
集合:
nx = {10,20,30} #只写{}的时候被认为是定义了一个空字典
ny = set()
#没有顺序,集合内不允许元素重复
|