namelist = [] #定义一个空的列表
namelist1 = ["小张","小红","小李"]
print(namelist1[0])
print(namelist1[1])
print(namelist1[2])
testlist = [1,"测试"] #列表中存储混合类型
print(type(testlist[0]))
print(type(testlist[1]))
'''
'''
# for循环遍历输出列表
namelist1 = ["小张","小红","小李"]
for name in namelist1:
print(name)
length = len(namelist1) #获取列表长度
#while循环遍历输出列表
i = 0
while i < length:
print(namelist1[i])
i += 1
'''
#列表的增删改查
'''
#增 append
namelist = ["小张","小王","小赵"]
print("--------追加前名单列表的数据:----------")
for name in namelist:
print(name)
nametemp = input("请输入追加的姓名:")
namelist.append(nametemp) #在末尾追加一个元素
print("--------追加后名单列表的数据:----------")
for name in namelist:
print(name)
'''
'''
#增 extend
a = [1,2]
b = [3,4]
a.append(b) #将b列表作为一个元素
print(a)
a.extend(b) #将b列表扩充到a
print(a)
'''
'''
#增 insert
a = [1,2,3]
a.insert(1,"hhhhh") #下标,元素
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)
'''
#改 =
#查 in not in 查看指定元素是不是在列表中,并不是搜索出来
'''
namelist1 = ["小张","小红","小李"]
findName = input("请输入你要查找的名字:")
if findName in namelist1:
print("所查询名字在列表中")
else:
print("未查询到要搜索的名字")
'''
'''
letter = ["a","b","a","c","e","d"]
print(letter.index("a",2,4)) #在某一范围内查找某元素,并指出其下标,不在范围内会报错,左闭右开
letter.index("f",0,4)
print(letter.count("d")) #统计某个元素出现的次数
'''
'''
#排序和反转
a = [0,1,3,2]
print(a)
a.reverse() #将列表所有元素反转 注:列表被改变了!
print(a)
a.sort()
print(a) #升序
a.sort(reverse=True) #降序
print(a)
'''
'''
#schoolName = [[],[],[]] #有三个元素的空列表,每个元素都是一个空列表
schoolName = [["北京大学","清华大学"],["石家庄铁道大学","河北师范大学","河北大学"],["天津大学","南开大学"]]
print(schoolName[0][1])
'''
#将八位老师随机分配到三间办公室,并将各办公室名单打印出来
import random
offices = [[],[],[]]
tName = ["a","b","c","d","e","f","g","h"]
i = 0
while i < 8:
offices[random.randint(0,2)].append(tName[i])
i += 1
num = 1
print(offices)
for office in offices:
print("办公室%d的人数为%d"%(num,len(office)))
num += 1
print("教师的姓名为")
for name in office:
print("%s"%(name),end="\t")
print()
print("-"*20)
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python爬虫学习——列表 - Python技术站