shuffle什么意思?
除了翻译成“拖曳摇摆地走;搅乱;推诿,推卸”,还有洗牌的意思,随机但不重复。
Shuffle the cards and deal out five to each player.
Python random 模块除了有以下常用的函数外:
random() 返回0<=n<1之间的随机实数n;
choice(seq) 从序列seq中返回随机的元素;
getrandbits(n) 以长整型形式返回n个随机位;
sample(seq, n) 从序列seq中选择n个随机且独立的元素。
还有一个“随机发牌”函数: shuffle():
>>> from random import shuffle >>> n = [2, 3, 4, 5, 6, 7, 8, 9] >>> shuffle(n) >>> n [7, 5, 6, 2, 4, 9, 3, 8] >>> shuffle(n) >>> n [4, 6, 2, 3, 7, 8, 5, 9] >>>
直接随机打乱原序列的顺序排列,刚好可以用它来模拟发牌。
一副扑克牌54张,四种花色,每种13张,另加大小王各一张。
三人斗地主用一副,掼蛋则是四人用两副牌,每人27张牌。
扑克牌表示如下(其中10用字母T来表示):
>>> F = ('♠', '♥', '♣', '♦') >>> V = tuple(str(i) for i in range(2,10))+('T','J','Q','K','A') >>> V ('2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A') >>> >>> # 或者: >>> V = (*(str(i) for i in range(2,10)),*tuple('TJQKA')) >>> V = (*[str(i) for i in range(2,10)],*list('TJQKA'))
大小王分别用 Rj, Bj 表示:
>>> P = [f+v for f in F for v in V] + ['Rj','Bj'] >>> P ['♠2', '♠3', '♠4', '♠5', '♠6', '♠7', '♠8', '♠9', '♠T', '♠J', '♠Q', '♠K', '♠A', '♥2', '♥3', '♥4', '♥5', '♥6', '♥7', '♥8', '♥9', '♥T', '♥J', '♥Q', '♥K', '♥A', '♣2', '♣3', '♣4', '♣5', '♣6', '♣7', '♣8', '♣9', '♣T', '♣J', '♣Q', '♣K', '♣A', '♦2', '♦3', '♦4', '♦5', '♦6', '♦7', '♦8', '♦9', '♦T', '♦J', '♦Q', '♦K', '♦A', 'Rj', 'Bj'] >>>
用shuffle()函数随机洗牌:
>>> from random import shuffle >>> shuffle(P) >>> P ['♦T', '♦6', '♣6', '♥2', '♥4', '♠6', '♠A', 'Rj', '♠J', '♥7', '♣2', '♥K', '♣4', '♦3', '♦4', '♦9', '♥A', '♠3', '♣K', '♣8', '♣J', '♠4', '♠K', '♦5', '♦K', '♥8', '♣7', '♠8', '♥6', '♥9', '♦A', '♣T', '♣9', '♣5', '♠Q', '♦2', '♠5', '♠T', '♣A', '♥3', '♦8', '♠2', '♥T', '♦Q', '♦7', '♥Q', 'Bj', '♦J', '♠9', '♥5', '♣3', '♠7', '♣Q', '♥J'] >>> shuffle(P) >>> P ['♠T', '♦4', '♥4', '♣2', '♣J', '♠8', '♥T', '♣3', '♥Q', '♣8', '♣4', '♦6', '♣Q', '♦5', '♥7', '♦T', '♥A', '♠9', '♥9', '♣5', '♦2', '♠7', '♠Q', '♦7', '♣K', '♦3', '♠6', '♦K', '♥3', '♣6', '♥6', 'Rj', '♣7', 'Bj', '♥2', '♦9', '♥8', '♠4', '♥K', '♦8', '♣T', '♠2', '♠3', '♣9', '♠A', '♦J', '♥5', '♣A', '♦Q', '♠5', '♦A', '♠J', '♠K', '♥J'] >>>
分发一副扑克牌成3堆:
>>> C = [P[i:i+18] for i in range(0,54,18)] >>> for c in C:print(c) ['♠T', '♦4', '♥4', '♣2', '♣J', '♠8', '♥T', '♣3', '♥Q', '♣8', '♣4', '♦6', '♣Q', '♦5', '♥7', '♦T', '♥A', '♠9'] ['♥9', '♣5', '♦2', '♠7', '♠Q', '♦7', '♣K', '♦3', '♠6', '♦K', '♥3', '♣6', '♥6', 'Rj', '♣7', 'Bj', '♥2', '♦9'] ['♥8', '♠4', '♥K', '♦8', '♣T', '♠2', '♠3', '♣9', '♠A', '♦J', '♥5', '♣A', '♦Q', '♠5', '♦A', '♠J', '♠K', '♥J'] >>>
两副扑克洗牌后分发成4堆:
>>> from random import shuffle >>> F = ('♠', '♥', '♣', '♦') >>> V = tuple(str(i) for i in range(2,10))+('T','J','Q','K','A') >>> P = [f+v for f in F for v in V] + ['Rj','Bj'] >>> P = P * 2 >>> shuffle(P) >>> C = [P[i:i+27] for i in range(0,108,27)] >>> for c in C:print(c) ['♠J', 'Rj', '♦3', '♦6', '♥Q', 'Bj', '♠J', '♣K', '♣Q', '♥4', 'Rj', '♦4', '♠4', '♣J', '♣2', '♠A', '♠9', '♣A', '♦K', '♥8', '♥3', '♦5', '♠4', '♦7', '♠6', '♠T', '♠7'] ['♠5', '♥6', '♥J', '♦7', '♣5', '♦2', '♥T', '♣9', '♠9', '♦3', '♦6', '♦8', '♦Q', '♥2', '♥8', '♣Q', '♦T', '♠2', '♣7', '♥4', '♠Q', '♦5', '♥9', '♣A', '♥K', '♦4', '♣6'] ['♥J', '♠3', '♠7', '♣6', '♥T', '♣7', '♥3', '♦9', '♦8', '♣8', '♥6', '♣T', '♠K', '♣K', '♥5', '♦9', '♠6', '♣4', '♦A', '♥7', '♠8', '♣2', '♣3', '♦T', '♥7', '♠T', '♦K'] ['♣4', '♠3', '♥5', '♥K', '♠8', '♣3', '♠2', '♦Q', '♣T', '♥2', '♦J', 'Bj', '♣8', '♦2', '♠A', '♥A', '♥A', '♦J', '♠Q', '♠5', '♣J', '♥Q', '♣9', '♠K', '♣5', '♦A', '♥9'] >>>
扑克牌分别按花色和大小排序:
>>> sorted(C[0]) ['Bj', 'Rj', 'Rj', '♠4', '♠4', '♠6', '♠7', '♠9', '♠A', '♠J', '♠J', '♠T', '♣2', '♣A', '♣J', '♣K', '♣Q', '♥3', '♥4', '♥8', '♥Q', '♦3', '♦4', '♦5', '♦6', '♦7', '♦K'] >>> >>> sorted([c[1]+c[0] for c in C[0]]) ['2♣', '3♥', '3♦', '4♠', '4♠', '4♥', '4♦', '5♦', '6♠', '6♦', '7♠', '7♦', '8♥', '9♠', 'A♠', 'A♣', 'J♠', 'J♠', 'J♣', 'K♣', 'K♦', 'Q♣', 'Q♥', 'T♠', 'jB', 'jR', 'jR'] >>>
10以上的大小排序:
>>> C[0] ['♠J', 'Rj', '♦3', '♦6', '♥Q', 'Bj', '♠J', '♣K', '♣Q', '♥4', 'Rj', '♦4', '♠4', '♣J', '♣2', '♠A', '♠9', '♣A', '♦K', '♥8', '♥3', '♦5', '♠4', '♦7', '♠6', '♠T', '♠7'] >>> V = [10+'TJQKAj'.index(c[0]) if c[0] in 'TJQKAj' else ord(c[0])-48 for c in [c[1] for c in C[0]]] >>> V # 把牌面大小转化成数字 [11, 15, 3, 6, 12, 15, 11, 13, 12, 4, 15, 4, 4, 11, 2, 14, 9, 14, 13, 8, 3, 5, 4, 7, 6, 10, 7] >>> S = [(C[0][i],V[i]) for i in range(27)] >>> S [('♠J', 11), ('Rj', 15), ('♦3', 3), ('♦6', 6), ('♥Q', 12), ('Bj', 15), ('♠J', 11), ('♣K', 13), ('♣Q', 12), ('♥4', 4), ('Rj', 15), ('♦4', 4), ('♠4', 4), ('♣J', 11), ('♣2', 2), ('♠A', 14), ('♠9', 9), ('♣A', 14), ('♦K', 13), ('♥8', 8), ('♥3', 3), ('♦5', 5), ('♠4', 4), ('♦7', 7), ('♠6', 6), ('♠T', 10), ('♠7', 7)] >>> >>> S1 = sorted(S,key=lambda x:x[1]) >>> S1 [('♣2', 2), ('♦3', 3), ('♥3', 3), ('♥4', 4), ('♦4', 4), ('♠4', 4), ('♠4', 4), ('♦5', 5), ('♦6', 6), ('♠6', 6), ('♦7', 7), ('♠7', 7), ('♥8', 8), ('♠9', 9), ('♠T', 10), ('♠J', 11), ('♠J', 11), ('♣J', 11), ('♥Q', 12), ('♣Q', 12), ('♣K', 13), ('♦K', 13), ('♠A', 14), ('♣A', 14), ('Rj', 15), ('Bj', 15), ('Rj', 15)] >>> S2 = sorted(S,key=lambda x:x[1],reverse=True) >>> S2 [('Rj', 15), ('Bj', 15), ('Rj', 15), ('♠A', 14), ('♣A', 14), ('♣K', 13), ('♦K', 13), ('♥Q', 12), ('♣Q', 12), ('♠J', 11), ('♠J', 11), ('♣J', 11), ('♠T', 10), ('♠9', 9), ('♥8', 8), ('♦7', 7), ('♠7', 7), ('♦6', 6), ('♠6', 6), ('♦5', 5), ('♥4', 4), ('♦4', 4), ('♠4', 4), ('♠4', 4), ('♦3', 3), ('♥3', 3), ('♣2', 2)] >>> [s[0] for s in S2] ['Rj', 'Bj', 'Rj', '♠A', '♣A', '♣K', '♦K', '♥Q', '♣Q', '♠J', '♠J', '♣J', '♠T', '♠9', '♥8', '♦7', '♠7', '♦6', '♠6', '♦5', '♥4', '♦4', '♠4', '♠4', '♦3', '♥3', '♣2'] >>> [s[0] for s in S1] ['♣2', '♦3', '♥3', '♥4', '♦4', '♠4', '♠4', '♦5', '♦6', '♠6', '♦7', '♠7', '♥8', '♠9', '♠T', '♠J', '♠J', '♣J', '♥Q', '♣Q', '♣K', '♦K', '♠A', '♣A', 'Rj', 'Bj', 'Rj'] >>>
整合成一行代码:
>>> [s[0] for s in sorted([(C[0][i],[10+'TJQKAj'.index(c[0]) if c[0] in 'TJQKAj' else ord(c[0])-48 for c in [c[1] for c in C[0]]][i]) for i in range(27)],key=lambda x:x[1])] ['♣2', '♦3', '♥3', '♥4', '♦4', '♠4', '♠4', '♦5', '♦6', '♠6', '♦7', '♠7', '♥8', '♠9', '♠T', '♠J', '♠J', '♣J', '♥Q', '♣Q', '♣K', '♦K', '♠A', '♣A', 'Rj', 'Bj', 'Rj'] >>>
完整的2副牌洗牌发牌代码:
>>> from random import shuffle >>> P = [f+v for f in ('♠','♥','♣','♦') for v in ([str(i) for i in range(2,10)]+list('TJQKA'))*2]+['Rj','Bj']*2 >>> shuffle(P) >>> C = [P[i:i+27] for i in range(0,108,27)] >>> Player = lambda p:[s[0] for s in sorted([(p[i],[10+'TJQKAj'.index(c[0]) if c[0] in 'TJQKAj' else ord(c[0])-48 for c in [c[1] for c in p]][i]) for i in range(27)],key=lambda x:x[1])] >>> for i in range(4):Player(C[i]) ['♣2', '♦3', '♣4', '♣4', '♠4', '♠5', '♣5', '♠5', '♥6', '♣6', '♦8', '♦8', '♥8', '♣9', '♦9', '♥9', '♦T', '♥T', '♠J', '♦J', '♣Q', '♠K', '♣A', '♠A', '♠A', 'Rj', 'Rj'] ['♥2', '♦2', '♣3', '♦3', '♠3', '♦4', '♥5', '♦6', '♠6', '♥6', '♦6', '♥7', '♥7', '♣8', '♠8', '♣8', '♥9', '♣9', '♦T', '♣J', '♠J', '♦J', '♥Q', '♥K', '♥K', '♦A', 'Bj'] ['♠2', '♠2', '♣2', '♣3', '♥3', '♥4', '♥4', '♦4', '♥5', '♦7', '♣7', '♠7', '♠7', '♠9', '♥T', '♠T', '♣T', '♣T', '♣J', '♦Q', '♠Q', '♦Q', '♣K', '♠K', '♦A', '♥A', '♥A'] ['♦2', '♥2', '♥3', '♠3', '♠4', '♦5', '♣5', '♦5', '♠6', '♣6', '♣7', '♦7', '♠8', '♥8', '♠9', '♦9', '♠T', '♥J', '♥J', '♠Q', '♥Q', '♣Q', '♦K', '♣K', '♦K', '♣A', 'Bj'] >>> >>> >>> #再次发牌: >>> def Poker(P): shuffle(P) for i in range(4):print(Player([P[i:i+27] for i in range(0,108,27)][i])) >>> Poker(P) ['♣2', '♥2', '♠3', '♦3', '♦3', '♦4', '♣6', '♣6', '♦7', '♥7', '♦8', '♦9', '♣9', '♠9', '♣T', '♦J', '♣J', '♠J', '♥J', '♦Q', '♣Q', '♥Q', '♣K', '♠K', '♥A', 'Rj', 'Bj'] ['♣2', '♥2', '♣3', '♥4', '♣4', '♥5', '♦5', '♥5', '♠7', '♣8', '♥8', '♣8', '♠8', '♦9', '♥T', '♠T', '♠T', '♦T', '♥J', '♣J', '♦Q', '♥Q', '♠K', '♠A', '♠A', '♥A', '♦A'] ['♠2', '♠4', '♣5', '♠5', '♣5', '♠6', '♥6', '♠6', '♥6', '♦6', '♣7', '♠7', '♦7', '♥8', '♠8', '♦8', '♠9', '♥9', '♣T', '♥T', '♦J', '♠Q', '♥K', '♦K', '♣A', '♣A', 'Bj'] ['♠2', '♦2', '♦2', '♣3', '♥3', '♠3', '♥3', '♥4', '♠4', '♣4', '♦4', '♠5', '♦5', '♦6', '♥7', '♣7', '♣9', '♥9', '♦T', '♠J', '♣Q', '♠Q', '♣K', '♦K', '♥K', '♦A', 'Rj'] >>> Poker(P) ['♥4', '♦4', '♣4', '♣4', '♣5', '♦5', '♦5', '♦7', '♥7', '♠8', '♦8', '♣8', '♠8', '♣9', '♠9', '♦T', '♠T', '♠T', '♦T', '♥J', '♥J', '♥Q', '♦Q', '♥K', '♥K', '♣K', '♦K'] ['♣2', '♦2', '♠3', '♥3', '♦3', '♠5', '♥6', '♠6', '♠6', '♥6', '♠7', '♥8', '♦8', '♣8', '♥9', '♠9', '♣9', '♦J', '♦Q', '♥Q', '♣Q', '♣A', '♠A', '♥A', '♠A', 'Rj', 'Rj'] ['♠2', '♣2', '♥2', '♠3', '♣3', '♦4', '♠4', '♥4', '♥5', '♣6', '♦6', '♣6', '♠7', '♦7', '♦9', '♦9', '♣T', '♣T', '♣J', '♦J', '♠J', '♠Q', '♠K', '♣K', '♣A', '♦A', 'Bj'] ['♠2', '♥2', '♦2', '♣3', '♦3', '♥3', '♠4', '♠5', '♥5', '♣5', '♦6', '♣7', '♣7', '♥7', '♥8', '♥9', '♥T', '♥T', '♠J', '♣J', '♠Q', '♣Q', '♠K', '♦K', '♦A', '♥A', 'Bj']
最后一次发牌,Player1家有4,8,10,K四个炸弹!^_^