列表代码如下(示例):
# -*- codeing = utf-8 -*-
# @Time : 2021/4/16 15:26
# @Author : M201874704
# @File : 列表.py
# @Software :PyCharm
'''
namelist = ["小张","小王","小李"]
testlist = [1,"测试"] #列表中存储混合类型
print(testlist[0])
print(testlist[1])
print(namelist[0])
print(namelist[1])
print(namelist[2])
for name in namelist:
print(name)
for number in range(0,len(namelist)):
print(number)
'''
'''
#增加
namelist = ["小张","小王","小李"]
print("---增加前,列表数据---")
for name in namelist:
print(name)
nametemp = input("请输入学生姓名:")
namelist.append(nametemp) #在末尾加元素
print("---增加后,列表数据---")
for name in namelist:
print(name)
'''
'''
a = [1,2]
b = [3,4]
a.append(b) #将列表当做一个元素,加入列表中
print(a)
a.extend(b) #将列表中的元素逐一加入
print(a)
'''
'''
#曾:[insert]
a = [0,1,2]
a.insert(1,3) #第一个变量表示下标,第二个表示元素(对象)
print(a) #指定下标位置插入元素
'''
'''
#删 [del] [pop] [remove]指定位置删除
movieName = ["加拉比海盗","第一滴血","指环王","速度与激情","指环王"]
print("---删除前,电影列表数据---")
for name in movieName:
print(name)
#del movieName[2] 在指定位置删除一个元素
#movieName.pop() #弹出最后一个元素
movieName.remove("指环王") #删除指定元素
print("---删除后,列表数据---")
for name in movieName:
print(name)
'''
'''
#改
namelist = ["小张","小王","小李"]
print("---修改前,列表数据---")
for name in namelist:
print(name)
namelist[1] = '小红' #指定下标的内容
print("---修改后,列表数据---")
for name in namelist:
print(name)
'''
'''
#查: [in,not in ]
namelist = ["小张","小王","小李"]
findName = input("请输入你要查找的学生姓名在不在?")
if findName in namelist:
print("在学员名单里找到")
else:
print("在学员名单里没找到")
'''
'''
mylist = ["a","b","c","a","e"]
print(mylist.index("a",1,4)) #可以查找指定下标范围的元素,并返回对应数据的下标
print(mylist.index("a",1,3)) #范围区间左闭右开,找不到会报错
print(mylist.count("a"))
'''
#排序与反转
'''
a = [1,4,2,3]
print(a)
a.reverse() #将列表所有元素反转
print(a)
a.sort() #升序
print(a)
a.sort(reverse = True) #降序
print(a)
'''
'''
#schoolNames = [[],[],[]] #有3个元素的空列表,每个元素都是空列表
schoolNames = [["北京大学","清华大学"],["北京大学","清华大学","武汉大学"],["北京大学","清华大学","武汉大学","华中科技"]]
print(schoolNames[0])
print(schoolNames[0][0])
'''
'''
import random
offices = [[],[],[]]
names = ["A","B","C","D","E","F","G","H"]
for name in names:
index = random.randint(0,2)
offices[index].append(name)
i =1
for office in offices:
print("办公室认为:%d %d" %(i,len(office)))
i +=1
for name in office:
print("%s"%name,end = "\t")
print("\n")
print("-"*20)
'''
products = [["iphone",6888],["MacPro",14800],["小米6",2499],["Coffee",31],["Book",60],["Nike",699]]
print("-----商品列表-----")
for i in range(0,len(products)):
print("%d %s %d" % (i,products [i][0],products [i][1]))
sum = []
price = 0
number = input("用户想买什么:")
while number !="q":
sum.append(products [int(number)])
number = input("用户想买什么:")
for a in range(0,len(sum)):
print("%d %s %d" % (a,sum [a][0],sum [a][1]))
price +=sum [a][1]
print(price)