list = [] #定义空列表 list1 = ["小张","小王","小李"] #for循环遍历获取列表数据 #列表:增append,extend,insert
list1 = ["小张","小王","小李"]
list2 = [1,"菜J学Python"] #列表中可以存储混合类型
for list in list1:
print(list)
'''
list1 = ["小张","小王","小李"] #while循环遍历获取列表数据
length = len(list1)
i = 0
while i<length:
print(list1[i])
i += 1
'''
'''
list1 = ["小张","小王","小李"]
print("---增加前列表数据----")
for list in list1:
print(list)
addname = input("请输入添加的学生姓名:")
list1.append(addname) #append为在末尾追加一个元素
print("---增加后列表数据----")
for list in list1:
print(list)
'''
'''
a = [1,2]
b = [3,4]
a.append(b) #将列表当做一个元素加入到列表中
print(a)
a.extend(b) #将列表中的每个元素逐一追加到列表中
print(a)
'''
'''
a = [0,1,2]
a.insert(1,3) #指定下标位置1插入元素3
print(a)
'''
4.列表:删
#列表:删del,pop,remove
'''
list1 = ["小张","小王","小李"]
print("---删除前列表数据----")
for list in list1:
print(list)
#del list1[2] #del为在指定位置删除一个元素
#list1.pop() #pop为弹出末尾最后一个元素
#list1.remove("小张") #remove直接删除指定内容的元素(如有重复只删除第一个)
print("---删除后列表数据----")
for list in list1:
print(list)
'''
5.列表:改
#列表:改
'''
list1 = ["小张","小王","小李"]
print("---删除前列表数据----")
for list in list1:
print(list)
list1[2] = "小潘" #指定下标修改
print("---删除后列表数据----")
for list in list1:
print(list)
'''
6.列表:查
#列表:查in
ot in
'''
list1 = ["小张","小王","小李"]
findname = input("请输入你要查找的姓名")
if findname in list1:
print("在名单中找到了你查找的姓名")
else:
print("没有找到")
'''
'''
list = ["a","b","c","a","b"]
print(list.index("a",1,4)) #可以查找指定下标范围的元素,并返回找到对应数据的下标
print(list.index("a",1,3)) #因为范围区间左闭右开【1,3),找不到会报错
'''
'''
list = ["a","b","c","a","b"]
print(list.count("a")) #统计元素出现的次数
'''
7.列表:其他
#列表:排序与反转sort
everse
'''
a = [1,4,2,3]
print(a)
a.reverse() #将列表所有元素反转
print(a)
a.sort() #升序
print(a)
a.sort(reverse=True) #降序
print(a)
'''
#列表:嵌套 list=[[],[],[]]
'''
schoolname = [["北京大学","清华大学"],["南开大学","天津大学","天津财经大学"],["山东大学","中国海洋大学"]]
print(schoolname[0])
print(schoolname[0][0])
'''
#列表:使用枚举函数enumerate,同时拿到列表中的下标和元素内容
'''
mylist = ["a","b","c","d"]
for i,x in enumerate(mylist):
print(i+1,x)
'''
#列表:将8人中的任意三个人随机分配到3个办公室
'''
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=" ")
print("
")
print("-"*28)
'''