zoukankan      html  css  js  c++  java
  • 一摞python风格的纸牌

    创建了一个简单的类FrenchDeck
    import collections

    Card =collections.namedtuple('Card',['rank','suit'])
    class FrenchDeck:
    ranks =[str(n) for n in list(range(2,11))+list('JQKA')] #python3中需要使用list来定义该数据
    suits ='spades diamonds clubs hearts'.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, item):
    return self._cards[item]
    实现类中的方法
    from mine.__init__ import  *
    from random import choice

    beer_Card = Card('7','diamonds')
    print(beer_Card)  

    deck =FrenchDeck()
    print(len(deck)) 》》》52

    print(deck[0])  
    print(deck[-1])
    print(deck[:3])
    print(deck[12::13])
    print(choice(deck))   

    for card in deck: #doctest:+ELLIPSIS
    print(card) #正向迭代
    # print(c)
    for card in reversed(deck): #反向迭代
    print(card)

    print(Card('Q','hearts') in deck)
    print(Card('7','beasts') in deck) #beasts 野兽

    suit_values = dict(spades =3,hearts =2 ,diamonds=1,clubs=0)
  • 相关阅读:
    Naive Operations HDU6315 (杭电多校2G)
    Baker Vai LightOJ
    LOJ#6278. 数列分块入门 2
    LOJ#6277. 数列分块入门 1
    Picture POJ
    Who Gets the Most Candies? POJ
    Dividing the Path POJ
    Balanced Sequence HDU
    归并排序
    Flying to the Mars
  • 原文地址:https://www.cnblogs.com/RENQIWEI1995/p/8203885.html
Copyright © 2011-2022 走看看