python的列表嵌套应用

本文原创,版权属作者个人所有,如需转载请联系作者本人。Q&微:155122733

--------------------------------------------------------------------------------------------------------

随机将8位老师分配到3个办公室,打印每个办公室的老师名字

#!/usr/bin/python
import random
#先定义一个列表,用来存储8个老师的名字 
names=['zhang','wang','li','zhao','jia','song','zheng','yang']
#定义一个列表,存在3个空的办公室,用于分配8个老师
offices = [[],[],[]]
#通过取随机数,获取一个随机的办公室号
#通过循环的方式,把8个老师随机分配到办公室中
for name in names:
        index = random.randint(0,2)
        offices[index].append(name)
#print(offices)
i = 1
for room in offices:
        #print(room)
        print("office %d------"%i)
        for teachername in room:
                print(teachername)
        i+=1

 如何保证每个办公室至少有两个老师?

待续

原文地址:https://www.cnblogs.com/lcl0421/p/8080839.html