把8位老师随机分配到3个办公室,random.randint(0, 2)

'''把8位老师随机分配到3个办公室'''
import random
teach = list("ABCDEFGH")
off = [[], [], []]
for i in teach:
    j = random.randint(0, 2)  # 随机三个数 0 1 2
    off[j].append(i)
print(off)

  

 

random.randint(0, 2) 产生三个数 0 1 2,包含末尾元素。 与range不同

off[j].append(i)  不能换成   off[j].extend(i)。如果用extend,现实中的名字“Tom”会被拆分为  ‘T’   ‘ o’    ‘m’  三个元素追加到列表

 

posted @ 2023-05-18 11:42  sangern  阅读(99)  评论(0)    收藏  举报