列表 list[ ] 作用 -- 存储多个值,多个元素
索引 list[num]
切片 list[:3]
追加 list.append('lalaal')
删除 list.pop() 删除最后一位 list.pop(num) 指定下标
list.remove('lalala') 删除指定值
长度 len(list) 统计列表里面元素个数
包含 in x in list 判断x是否在列表中
##其他操作
清空列表 list.clear()
复制列表 list.copy()
统计元素重复次数 list.count('lala')
添加多个值 list.extend(['la1','la2','la3'])
查下标 list.index('la')
列表反转 list.reverse()
列表排序 list.sort()
队列: 先进先出
入队 .append fifo=[]
fifo.append('一')
fifo.append('二')
fifo.append('三')
出队.pop(0)
fifo1 = fifo.pop(0)
fifo2 = fifo.pop(0)
fifo3 = fifo.pop(0)
入队.insert(指定位置插入):
fifo.insert(0,'一')
fifo.insert(0,'二')
fifo.insert(0,'三')
出队 .pop()
fifo.pop()
堆栈:先进后出
列表元素赋值
list=['la', 18, [1988, 3, 31]]
name, age, birth = list
print(name)
print(age)
print(birth)
===============
元组 tuple() -- 就是一个不可变的列表,主要是用来读操作,是可以当做字典的key的
####小练习:买商品加入到购物车,商品信息为字典,购物车信息为列表,选中的商品信息为元组(商品名称、单价、数量)。
循环字典:
msg_dic={
'apple':10,
'benz':100000,
'mac':3000,
'lenovo':30000,
'pen':10,
}
for i in msg_dic:
print(i,msg_dic[i]
1 ##商品字典 mag_dic{} 2 msg_dic={ 3 'apple':10, 4 'tesla':100000, 5 'mac':3000, 6 'lenovo':30000, 7 'chicken':10, 8 } 9 #购物车列表 goods_l[] 10 goods_l = [] 11 # for i in msg_dic: 12 # print(i,msg_dic[i]) 13 14 while True: 15 for key in msg_dic: #循环显示商品信息 16 print('