如何编写这个Python代码更短?

时间:2013-12-05 13:56:20

标签: python for-loop blackjack

我正在制作modified blackjack game。只能使用2到9张牌。在这个牌组中没有数十张,面牌或A牌。

首先,我创建了变量套牌,它采用了一个空列表 然后我创建了4 for loops以将卡片列表附加到卡片组。

我想知道是否有更短的方式来编写以下内容:

import random

deck = []
for list_of_cards in range(2, 10):
    spades = 'spades'
    list_of_spades = str(list_of_cards) + ' of' + ' ' + spades
    deck.append(list_of_spades)
for list_of_cards in range(2, 10):
    clubs = 'clubs'
    list_of_clubs = str(list_of_cards) + ' of' + ' ' + clubs
    deck.append(list_of_clubs)
for list_of_cards in range(2, 10):
    diamonds = 'diamonds'
    list_of_diamonds = str(list_of_cards) + ' of' + ' ' + diamonds
    deck.append(list_of_diamonds)
for list_of_cards in range(2, 10):
    hearts = 'hearts'
    list_of_hearts = str(list_of_cards) + ' of' + ' ' + hearts
    deck.append(list_of_hearts)

结果就像我想要的那样:

['2 of spades', '3 of spades', '4 of spades', '5 of spades', '6 of spades', '7 of spades', '8 of spades', '9 of spades', '2 of clubs', '3 of clubs', '4 of clubs', '5 of clubs', '6 of clubs', '7 of clubs', '8 of clubs', '9 of clubs', '2 of diamonds', '3 of diamonds', '4 of diamonds', '5 of diamonds', '6 of diamonds', '7 of diamonds', '8 of diamonds', '9 of diamonds', '2 of hearts', '3 of hearts', '4 of hearts', '5 of hearts', '6 of hearts', '7 of hearts', '8 of hearts', '9 of hearts']

由于我是Python的新手,我假设有一种方法可以将它写得更短。谢谢你的帮助!

5 个答案:

答案 0 :(得分:5)

为什么需要4个循环?为什么你需要创建那些无用的字符串? (+ ' of' + ' ' + spades' of spades'相同...)(我还修了一些其他的东西)

deck = []
for n in range(2, 10):
    deck.append('%i of spades' % n)
    deck.append('%i of hearts' % n)
    deck.append('%i of diamonds' % n)
    deck.append('%i of clubs' % n)

或者,使用嵌套循环:

deck = []
for n in range(2, 10):
    for s in ['spades', 'hearts', 'diamonds', 'clubs']:
        deck.append('%i of %s' % (n, s))

答案 1 :(得分:4)

例如,您不需要为“of”和“”使用单独的字符串。您不需要单独的字符串来容纳“钻石”这个词。只需写下“钻石”。

那就是说,这是一个使用字符串格式化,列表理解和拆分的解决方案,而不是手动制作一套西装。

deck = ['%d of %s' % (face, suit) for suit in 'spades hearts clubs diamonds'.split() for face in range(2,10)]

答案 2 :(得分:2)

我会使用列表理解:

>>> suits = ["hearts","diamonds","clubs","spades"]
>>> deck = ["{} of {}".format(num, suit) for suit in suits for num in range(2,10)]
>>> len(deck)
32
>>> deck
['2 of hearts', '3 of hearts', '4 of hearts', '5 of hearts', '6 of hearts', '7 of hearts', '8 of hearts', '9 of hearts', '2 of diamonds', '3 of diamonds', '4 of diamonds', '5 of diamonds', '6 of diamonds', '7 of diamonds', '8 of diamonds', '9 of diamonds', '2 of clubs', '3 of clubs', '4 of clubs', '5 of clubs', '6 of clubs', '7 of clubs', '8 of clubs', '9 of clubs', '2 of spades', '3 of spades', '4 of spades', '5 of spades', '6 of spades', '7 of spades', '8 of spades', '9 of spades']

此处的列表组合等同于

deck = []
for suit in suits:
    for num in range(2, 10):
        deck.append("{} of {}".format(num, suit))

答案 3 :(得分:1)

deck = []
colors = ["spades", "clubs", "diamonds", "hearts"]
for color in colors:
    for list_of_cards in range(2, 10):
        deck.append("{0} of {1}".format(list_of_cards, color))

答案 4 :(得分:1)

两个for循环就足够了:

deck = []
for suit in ('spades', 'hearts', 'diamonds', 'clubs'):
    for n in range(2, 10):
        deck.append('%i of %s' % (n, suit))

您也可以使用列表理解来编写它:

deck = ['%i of %s' % (n, suit) for suit in ('spades', 'hearts', 'diamonds', 'clubs') for n in range(2, 10)]