langchao 发表于 2020-8-30 19:57:49

编写随机分配数据到列表还能限制列表的个数

import random

offices = [[],[],[]]

names = ["A","B","D","E","F","G","H","I","J"]

for name in names:
    index = random.randint(0,2)
    offices.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')

这个代码要加入什么才能限制每个列表元素的个数

疾风怪盗 发表于 2020-8-30 20:20:26

for name in names:
    index = random.randint(0, 2)
    if len(offices)==3:
      continue
    offices.append(name)

不知道你是不是要这样的效果?

langchao 发表于 2020-8-30 20:24:06

疾风怪盗 发表于 2020-8-30 20:20
不知道你是不是要这样的效果?

对对 谢谢大佬

疾风怪盗 发表于 2020-8-30 20:30:45

langchao 发表于 2020-8-30 20:24
对对 谢谢大佬

问题解决了,给个最佳吧,把帖子关了
页: [1]
查看完整版本: 编写随机分配数据到列表还能限制列表的个数