循环5个组合中的3个

2024-04-19 10:12:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我在写扑克模拟游戏。我已经准备好一些零件了。 但我坚持把我的手和桌上的牌做比较。 我的想法是拿我的2张牌,从牌堆里随机拿3张牌,看看这会不会引起一个同花顺什么的。但我当然要循环,所以我要用手和套牌来做所有的组合。你知道吗

如果这是牌组中的5张牌,而1代表我将采取的牌位并与之比较。这些都是我要把我的牌和牌组比较的组合。你知道吗

00111
01011
01101
01110
10011
10101
10110
11001
11010
11100

我怎么能循环这个?桌牌只是一组物体。你知道吗


Tags: 代表物体扑克模拟游戏套牌桌牌牌位
2条回答

可能尝试来自itertoolscombinations

[c for c in itertools.combinations(range(5), 3)]

[(0, 1, 2),
 (0, 1, 3),
 (0, 1, 4),
 (0, 2, 3),
 (0, 2, 4),
 (0, 3, 4),
 (1, 2, 3),
 (1, 2, 4),
 (1, 3, 4),
 (2, 3, 4)]

您表示输出的方式令人困惑,但由于您要从一组5张卡中选择3张卡,因此需要5C3。您可以使用itertools.combinations实现这一点。你知道吗

doc

itertools.组合(iterable,r)

Return r length subsequences of elements from the input iterable.

Combinations are emitted in lexicographic sort order. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.

Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each combination.

导入itertools

a = [0,1,2,3,4]

print [p for p in itertools.combinations(a, 3)]

输出:

[(0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 2, 3), (0, 2, 4), (0, 3, 4),(1,2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]

相关问题 更多 >