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]

猜你喜欢

转载自blog.csdn.net/weixin_43935079/article/details/85799944