1. 程式人生 > >python中一些簡潔的用法

python中一些簡潔的用法

  1. 雙迴圈構造list 及具名元組
    import collections

Card = collections.namedtuple(‘Card’,[‘rank’, ‘suit’]) # 具名元組
ranks = [str(n) for n in range(2,11)] + list(‘JQKA’) # 牌數
suits = ‘spades hearts clubs diamonds’.split() # 牌色

cards = [Card(rank, suit) for suit in self.suits
for rank in self.ranks]