python - 为什么我的两个样本没有共同的 0 个数字?
问题描述
我编写了一个程序,可以对 0-9 之间的两个列表进行采样。然后我查看它们有多少个相同的数字,并将其发送到相应的计数器。我已经循环了这 1000000 个,但样本从来没有共同的 0 个数字。我的代码有问题,还是我非常不走运
for _ in range(1000000):
House_deal = random.sample(range(9), k=5)
Player_deal = random.sample(range(9), k=5)
hc = Counter(House_deal)
pc = Counter(Player_deal)
common = hc.keys() & pc.keys() #get the intersection of both keys
counts = 0
for cel in common:
counts += min(hc[cel], pc[cel])
if counts == 0:
common_0 += 1
elif counts == 1:
common_1 += 1
elif counts == 2:
common_2 += 1
elif counts == 3:
common_3 += 1
elif counts == 4:
common_4 += 1
elif counts == 5:
common_5 += 1
解决方案
我相信你想要做的是在 1000000 次的试验中确定最常见hc
的pc
。这样的事情应该做。您的主要问题是您没有在循环之外跟踪这些数字。我不确定count
是为了什么?
from random import sample
from collections import Counter
common = Counter()
for _ in range(1000000):
House_deal = sample(range(9), k=5)
Player_deal = sample(range(9), k=5)
common.update(set(House_deal).intersection(Player_deal))
推荐阅读
- python - 访问类中的特定值
- r - 1)如何按降序对列表中的多个表进行排序?2)如何从多个表的一个列表创建数据框?
- uml - 指定停车引导系统的 UML 类图?
- javascript - 如何从动态字段发送 POST 请求?
- angular - Angular 8 Firebase 获取 node_modules/firebase/index.d.ts(4369, 38): error TS1005: ';' 预期的
- python - 代码使用 x70 列的数据框和 1000 列的失败(相同的数据结构)
- r - 连接两个数据框并为连接列的缺失值添加 NA
- mysql - 导入表定义时忽略 ALTER TABLE 命令
- swift - 如何使用 Swift UI 在 Xcode 11.1 中更改按钮的大小
- python - cupy.split (cuda100-6.5.0) 返回a是否正常
代替 ?