将列表转换为元组

时间:2017-09-04 03:42:57

标签: python dictionary tuples

我有以下代码,它给出了以下错误:" TypeError:unhashable type:' Card'",其中card是一个定义套装和等级的类一副牌。例如,card.Card(2,2)对应于" 2的钻石。"我有下面的提示,其中"我必须将列表编码为元组,因为列表不能是字典键&#34 ;;任何想法如何实现这个?

def test_score():
    """Test _score function"""

    test_cases = {
        # Have to encode lists as tuples because lists can't be dictionary keys
        (card.Card(2, 2), card.Card(3, 5)):7, # regular cards
        (card.Card(2, 2), card.Card(3, 1)):13, # an ace
        (card.Card(1, 13), card.Card(0, 2)):12, # a king
        (card.Card(1, 1), card.Card(0, 1)):22, # two aces, busted list
        (card.Card(1, 2), card.Card(0, 3), card.Card(3, 4)):9, # three cards
        ():0 # empty list
    }

0 个答案:

没有答案