###MM的计算机解决方法 for i in range(10): import time print(time.strftime("%Y-%m-%d %X", time.localtime())) dou={1994:{'褐色':30,'黄色':20,'红色':20,'绿色':10,'橙色':10,'黄褐':10}, 1996:{'蓝色':24,'绿色':20,'橙色':16,'黄色':14,'红色':13,'褐色':13}} num=100000 #模拟抽取100万次,形成新的字典,键值1994,1996 list_1994=['褐色']*30*num+['黄色']*20*num+['红色']*20*num+['绿色']*10*num+['橙色']*10*num+['黄褐']*10*num list_1996=['蓝色']*24*num+['绿色']*20*num+['橙色']*16*num+['黄色']*14*num+['红色']*13*num+['褐色']*13*num #对列表随机排序 import random random.shuffle(list_1994) random.shuffle(list_1996) #遍历所有元素 count_all=0 count_key=0 for key in range(100*num): if list_1994[key]=='黄色' and list_1996[key]=='绿色': count_all+=1 count_key+=1 if list_1994[key]=='绿色' and list_1996[key]=='黄色': count_all+=1 print(count_key/count_all,20/27 ) print(time.strftime("%Y-%m-%d %X", time.localtime()))
#计算结果,发现和统计方法很接近,这个增加了数目,接下来采用模拟100万次,看下平均的结果和统计方法计算的结果差别
2018-01-17 17:49:54 0.7428241292665481 0.7407407407407407 2018-01-17 17:50:22 2018-01-17 17:50:22 0.7418338215119944 0.7407407407407407 2018-01-17 17:50:49 2018-01-17 17:50:49 0.7407099125094797 0.7407407407407407 2018-01-17 17:51:16 2018-01-17 17:51:16 0.7413828573565991 0.7407407407407407 2018-01-17 17:51:43 2018-01-17 17:51:43 0.7457792866763843 0.7407407407407407 2018-01-17 17:52:10 2018-01-17 17:52:10 0.7422211495618595 0.7407407407407407 2018-01-17 17:52:37 2018-01-17 17:52:37 0.7381602660572618 0.7407407407407407 2018-01-17 17:53:04 2018-01-17 17:53:04 0.7419709593841451 0.7407407407407407 2018-01-17 17:53:31 2018-01-17 17:53:31 0.7398747545114315 0.7407407407407407 2018-01-17 17:53:59 2018-01-17 17:53:59 0.7430824928885441 0.7407407407407407 2018-01-17 17:54:25
#模拟10万次代码及结果:
count_all=0 count_key=0 print(time.strftime("%Y-%m-%d %X", time.localtime())) for i in range(100000): print(i) import time dou={1994:{'褐色':30,'黄色':20,'红色':20,'绿色':10,'橙色':10,'黄褐':10}, 1996:{'蓝色':24,'绿色':20,'橙色':16,'黄色':14,'红色':13,'褐色':13}} num=1 #模拟抽取100万次,形成新的字典,键值1994,1996 list_1994=['褐色']*30*num+['黄色']*20*num+['红色']*20*num+['绿色']*10*num+['橙色']*10*num+['黄褐']*10*num list_1996=['蓝色']*24*num+['绿色']*20*num+['橙色']*16*num+['黄色']*14*num+['红色']*13*num+['褐色']*13*num #对列表随机排序 import random random.shuffle(list_1994) random.shuffle(list_1996) #遍历所有元素 key=random.choice(range(100)) if list_1994[key]=='黄色' and list_1996[key]=='绿色': count_all+=1 count_key+=1 if list_1994[key]=='绿色' and list_1996[key]=='黄色': count_all+=1 print(time.strftime("%Y-%m-%d %X", time.localtime())) print(count_key/count_all,20/27 )
#差距更大了一些