文章

一些你可能十分喜欢的Python小知识/语法糖--不段更新中

2020-08-17 21:43:28 星期一
#你看,split会自动分割所有的字符串
suits = '黑桃 方块 梅花 红桃'.split()
#print出来的结果为:
['黑桃', '方块', '梅花', '红桃']
#你看,可以这样迭代,那么被赋值的p首先会是一个列表,其次是str类型的n迭代出来的2-10
p = [str(n) for n in range(2, 11)]
#我们print一下出来的结果是:
['2', '3', '4', '5', '6', '7', '8', '9', '10']

#你还可以这样往列表里面添加非range元素
p = [str(n) for n in range(2, 11)] + list('JQKA')

['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']

#接下来你还可以酱紫
import collections

Card = collections.namedtuple('Card', ['rank','suit'])
class FrenchDeck:
/假设上边的代码隐藏在这里/
   def __init__(self):
        bius = [Card(rank, suit) for suit in self.suits for rank in self.ranks]
        for biu in bius:
            print(biu)
Card(rank='J', suit='黑桃')
Card(rank='Q', suit='黑桃')
Card(rank='K', suit='黑桃')
Card(rank='A', suit='黑桃')
Card(rank='2', suit='方块')
Card(rank='3', suit='方块')
Card(rank='4', suit='方块')
Card(rank='5', suit='方块')
Card(rank='6', suit='方块')
Card(rank='7', suit='方块')
Card(rank='8', suit='方块')
Card(rank='9', suit='方块')
Card(rank='10', suit='方块')
Card(rank='J', suit='方块')
Card(rank='Q', suit='方块')
...还有更多未打印

choice还可以让你随机抽一个元素出来!
首先导入包
from random import choice
choice(bius)
choice(bius)

Card(rank='3', suit='梅花')
Card(rank='J', suit='梅花')

run = FrenchDeck()
print(run[:3]) #抽最上边3张
print(run[12::13])#抽所有A(抽第12张,然后每隔13张抽一次)
[Card(rank='2', suit='黑桃'), Card(rank='3', suit='黑桃'), Card(rank='4', suit='黑桃')]
[Card(rank='A', suit='黑桃'), Card(rank='A', suit='方块'), Card(rank='A', suit='梅花'), Card(rank='A', suit='红桃')]

#你还可以正抽,反抽!as you wish
for card in run:
    print(card)
print('=' * 10)
for card in reversed(run):
    print(card)
...省略部分
Card(rank='K', suit='红桃')
Card(rank='A', suit='红桃')
==========
Card(rank='A', suit='红桃')
Card(rank='K', suit='红桃')
...省略部分
  • 下面我来给大家看一下这本书的不完整片段,自我感觉已经蛮绕了 - - ,真不知道这位神仙的逻辑.
import collections
from random import choice

Card = collections.namedtuple('Card', ['rank','suit'])

class FrenchDeck:
    ranks = [str(n) for n in range(2, 11)] + list('JQKA')
    suits = '黑桃 方块 梅花 红心'.split()


    def __init__(self):
        self._cards = [Card(rank, suit) for suit in self.suits for rank in self.ranks]

    def __len__(self):
        return len(self._cards)

    def __getitem__(self, position):
        return self._cards[position]

suit_values = dict(黑桃=1, 红心=2, 梅花=3, 方块=4)

def spades_high(card):
    rank_value = FrenchDeck.ranks.index(card.rank)#返回索引.index与python find()方法一样,只不过如果str不在 string中会报一个异常。
    # print(rank_value)#这里只有每种花色的12张牌,所以只有12个序号
    return rank_value * len(suit_values) + suit_values[card.suit]#这也是一个数字,对应每种花色的数字



run = FrenchDeck()

for card in sorted(run,key=spades_high):#sorted返回一个全新的列表,并进行排序,key是用来比较的对象,参数取自可迭代对象run
    print(card)
    #此时打印输出已经是按照顺序输出了,从小到大,我自己定义的花色大小顺序是黑桃>红心>梅花>方块
Card(rank='2', suit='黑桃')
Card(rank='2', suit='红心')
Card(rank='2', suit='梅花')
Card(rank='2', suit='方块')
Card(rank='3', suit='黑桃')
Card(rank='3', suit='红心')
Card(rank='3', suit='梅花')
Card(rank='3', suit='方块')
Card(rank='4', suit='黑桃')
Card(rank='4', suit='红心')
Card(rank='4', suit='梅花')
Card(rank='4', suit='方块')
...省略部分

原文来自:一些你可能十分喜欢的Python小知识/语法糖--不段更新中,尊重自己,尊重每一个人;转发请注明来源!
0 0

发表评论