标签:play highlight idt 行李箱 逗号 存在 key 出队 second
元组(tuple)
元组是不可变的列表,即元组的值不可更改,因此元组一般只用于只存不取的需求。也因此元组可以被列表取代掉,所以元组相比较列表使用的很少。元组相比较列表的优点为:列表的值修改后,列表的结构将会发生改变,而元组只需要存储,因此列表在某种程度上而言需要占用更多的内存。但是目前工业上内存已经不是问题了,所以工业上元组一般不会使用。
多个装备、多个爱好、多门课程等
# 1. 直接定义获得
test_tuple = ("zcy",)
test_tuple1 = ("zcy",20,["play","eat"])
# 2.利用tuple()方法转换列表
test_list = [1,2,3]
test_tuple = tuple(test_list)
查
因为列表属于不可变类型。所以不可进行增、删、改。只可进行查询的操作。方法同列表
# 1.元祖索引,这两个列表里的值代表各自位置索引
test_tuple1 = (0, 1, 2, 3, 4) # 从左是从0开始数
test_tuple2 = (-5,-4,-3,-2,-1) # 从右是-1开始数
# 2.取值
# 2.1 可以利用索引取值。
test_tuple = ("zcy",20,["play","eat"])
# 取出"zcy"
test_tuple[0]
# 取出20
test_tuple[-2]
# 取出"play"
test_tuple[2][0]
其他操作
| 方法 | 意义 |
| index(x) | 查询x在元祖中的索引值,无x则报错 |
| count(x) | 统计x在元祖中出现的次数 |
#for循环
# for k in d.keys(): # print(k) # # for k in d: # print(k) # for v in d.values(): # print(v) # for k,v in d.items(): # print(k,v) # print(list(d.keys())) # print(list(d.values())) # print(list(d.items()))
队列和堆栈
队列:
# 队列:LIFO,先进先出(上楼的那种自动扶梯) l = [] #入栈操作 l.append(‘first‘) l.append(‘second‘) l.append(‘third‘) print(l) #出队操作 print(l.pop(0)) print(l.pop(0)) print(l.pop(0)) # [‘first‘, ‘second‘, ‘third‘] # first # second # third
堆栈:
# 堆栈: LIFO,后进先出(在行李箱里面放衣服) l = [] #入栈操作 l.append(‘first‘) l.append(‘second‘) l.append(‘third‘) print(l) #出队操作 print(l.pop()) print(l.pop()) print(l.pop()) # [‘first‘, ‘second‘, ‘third‘] # third # second # first
#需要掌握的内置方法
# d={‘k1‘:111}
#1、d.clear()
#2、d.update()
# d.update({‘k2‘:222,‘k3‘:333,‘k1‘:111111111111111})
# print(d)
#3、d.get() :根据key取值,容错性好
# print(d[‘k2‘]) # key不存在则报错
标签:play highlight idt 行李箱 逗号 存在 key 出队 second
原文地址:https://www.cnblogs.com/Tornadoes-Destroy-Parking-Lots/p/12464611.html