列表的嵌套以及random使用

分配老师到不同的办公室

import random
offices = [[],[],[]]
names = ['A','B','C','D','E','F','G','F','H']
for name in names :
    index = random.randint(0,2)
    offices[index].append(name)
i = 1
for temp in offices :
    print ('办公室%d的人数为:%d'%(i,len(temp)))
    i+=1
    for name in temp :
        print ("%s"%name,end='')
    print ('-'*20)

随机整数: 

 import random

 random.randint(0,99)

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

随机选取0到100间的偶数:

random.randrange(0, 101, 2)

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

随机浮点数:
>>> import random
>>> random.random() 

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

随机字符:
>>> import random
>>> random.choice('abcdefg&#%^*f')

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

随机选取字符串:
>>> import random
>>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )

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

洗牌:

import random
items = [1,2,3,4,5,6,7,8]
random.shuffle(items)
print (items)

 

posted @ 2018-08-04 20:14  伽罗一刀  阅读(455)  评论(0)    收藏  举报