正在开展一个小型项目来进行二十一点游戏。我意识到有十亿如何做到这一点,但我正在努力理解编码并建立我自己的......据说,我搞砸了很多。虽然这个难题让我感到困惑......在设置我的套牌时,我尝试了这个:
suits = ['spades', 'hearts', 'clubs', 'diamonds']
ranks = ['ace', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'Jack', 'Queen', 'King']
decks = []
for suit in suits:
for rank in ranks:
decks += (rank, suit)
print decks
我得到两个列表一起排序的预期结果: ['ace','黑桃','两个','黑桃','三','黑桃','四'......]
但是,当我尝试将它们组合成字典时,如下所示:
b = dict(zip(decks[1::2], decks[0::2]))
print b
我得到:{'心':'国王','俱乐部':'国王','黑桃':'国王','钻石':'国王'}为什么它只做国王的价值观?
当我尝试使用以下代码纠正时:
spade = ['spades']
ranks = ['ace', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'Jack', 'Queen', 'King']
spades = []
for rank in ranks:
spades += (rank, spade)
print spades
我得到了这个输出: ['ace',['spades'],'two',['spades'],'three',['spades'],...]
那是什么给出的?帮助一个菜鸟!我的目的是通过列表创建一个优雅的套牌,将值附加到卡片上,创建一个dictonary并使用这些值来计算分数......并尝试更好地理解Python编码!
谢谢!
答案 0 :(得分:2)
谈论乘法列表应该强迫您使用itertools.product
,并通过转换为list
强制迭代
import itertools
suits = ['spades', 'hearts', 'clubs', 'diamonds']
ranks = ['ace', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'Jack', 'Queen', 'King']
decks = list(itertools.product(suits,ranks))
print(decks)
结果
[('spades', 'ace'), ('spades', 'two'), ('spades', 'three'), ('spades', 'four'), ('spades', 'five'), ('spades', 'six'), ('spades', 'seven'), ('spades', 'eight'), ('spades', 'nine'), ('spades', 'ten'), ('spades', 'Jack'), ('spades', 'Queen'), ('spades', 'King'), ('hearts', 'ace'), ('hearts', 'two'), ('hearts', 'three'), ('hearts', 'four'), ('hearts', 'five'), ('hearts', 'six'), ('hearts', 'seven'), ('hearts', 'eight'), ('hearts', 'nine'), ('hearts', 'ten'), ('hearts', 'Jack'), ('hearts', 'Queen'), ('hearts', 'King'), ('clubs', 'ace'), ('clubs', 'two'), ('clubs', 'three'), ('clubs', 'four'), ('clubs', 'five'), ('clubs', 'six'), ('clubs', 'seven'), ('clubs', 'eight'), ('clubs', 'nine'), ('clubs', 'ten'), ('clubs', 'Jack'), ('clubs', 'Queen'), ('clubs', 'King'), ('diamonds', 'ace'), ('diamonds', 'two'), ('diamonds', 'three'), ('diamonds', 'four'), ('diamonds', 'five'), ('diamonds', 'six'), ('diamonds', 'seven'), ('diamonds', 'eight'), ('diamonds', 'nine'), ('diamonds', 'ten'), ('diamonds', 'Jack'), ('diamonds', 'Queen'), ('diamonds', 'King')]
或者因为您似乎需要一个平面列表,只需使用chain.from_iterable
展平它:
decks = list(itertools.chain.from_iterable(itertools.product(suits,ranks)))
答案 1 :(得分:1)
您可以使用列表组合创建一组卡片,如[[rank,suits]]
。并且dict()
仅从2个值[value1,value2]
创建一个对象。您持续获得{'hearts': 'King', 'clubs': 'King', 'spades': 'King', 'diamonds': 'King'}
的原因是因为dict具有唯一的{key:value}
对,而King
数组中要为这些键分配的最后一项是ranks
。
suits = ['spades', 'hearts', 'clubs', 'diamonds']
ranks = ['ace', 'two', 'three', 'four', 'five', 'six', 'seven',
'eight', 'nine', 'ten', 'Jack', 'Queen', 'King']
print [(rank, suit) for suit in suits for rank in ranks]
您可以将密钥设为ranks
,将值(rank,suit)
的数组设为rank
card_dict = {}
suits = ['spades', 'hearts', 'clubs', 'diamonds']
ranks = ['ace', 'two', 'three', 'four', 'five', 'six', 'seven',
'eight', 'nine', 'ten', 'Jack', 'Queen', 'King']
deck = [(rank, suit) for suit in suits for rank in ranks]
map(
lambda rank: card_dict.update(
{
rank: filter(lambda card: rank in card, deck)
}
), ranks
)
print card_dict
{
'King': [('King', 'spades'), ('King', 'hearts'), ('King', 'clubs'), ('King', 'diamonds')],
'seven': [('seven', 'spades'), ('seven', 'hearts'), ('seven', 'clubs'), ('seven', 'diamonds')],
'Queen': [('Queen', 'spades'), ('Queen', 'hearts'), ('Queen', 'clubs'), ('Queen', 'diamonds')],
'ace': [('ace', 'spades'), ('ace', 'hearts'), ('ace', 'clubs'), ('ace', 'diamonds')],
'ten': [('ten', 'spades'), ('ten', 'hearts'), ('ten', 'clubs'), ('ten', 'diamonds')],
'nine': [('nine', 'spades'), ('nine', 'hearts'), ('nine', 'clubs'), ('nine', 'diamonds')],
'six': [('six', 'spades'), ('six', 'hearts'), ('six', 'clubs'), ('six', 'diamonds')],
'two': [('two', 'spades'), ('two', 'hearts'), ('two', 'clubs'), ('two', 'diamonds')],
'three': [('three', 'spades'), ('three', 'hearts'), ('three', 'clubs'), ('three', 'diamonds')],
'four': [('four', 'spades'), ('four', 'hearts'), ('four', 'clubs'), ('four', 'diamonds')],
'five': [('five', 'spades'), ('five', 'hearts'), ('five', 'clubs'), ('five', 'diamonds')],
'Jack': [('Jack', 'spades'), ('Jack', 'hearts'), ('Jack', 'clubs'), ('Jack', 'diamonds')],
'eight': [('eight', 'spades'), ('eight', 'hearts'), ('eight', 'clubs'), ('eight', 'diamonds')]
}
或者按suits
代替ranks
{
'clubs': [('ace', 'clubs'),
('two', 'clubs'),
('three', 'clubs'),
('four', 'clubs'),
('five', 'clubs'),
('six', 'clubs'),
('seven', 'clubs'),
('eight', 'clubs'),
('nine', 'clubs'),
('ten', 'clubs'),
('Jack', 'clubs'),
('Queen', 'clubs'),
('King', 'clubs')
],
'diamonds': [('ace', 'diamonds'),
('two', 'diamonds'),
('three', 'diamonds'),
('four', 'diamonds'),
('five', 'diamonds'),
('six', 'diamonds'),
('seven', 'diamonds'),
('eight', 'diamonds'),
('nine', 'diamonds'),
('ten', 'diamonds'),
('Jack', 'diamonds'),
('Queen', 'diamonds'),
('King', 'diamonds')
],
'hearts': [('ace', 'hearts'),
('two', 'hearts'),
('three', 'hearts'),
('four', 'hearts'),
('five', 'hearts'),
('six', 'hearts'),
('seven', 'hearts'),
('eight', 'hearts'),
('nine', 'hearts'),
('ten', 'hearts'),
('Jack', 'hearts'),
('Queen', 'hearts'),
('King', 'hearts')
],
'spades': [('ace', 'spades'),
('two', 'spades'),
('three', 'spades'),
('four', 'spades'),
('five', 'spades'),
('six', 'spades'),
('seven', 'spades'),
('eight', 'spades'),
('nine', 'spades'),
('ten', 'spades'),
('Jack', 'spades'),
('Queen', 'spades'),
('King', 'spades')
]
}
import random
random.shuffle(deck)
print deck