【问题标题】:Sorting list of tuples representing cards by alphabet and then rank按字母表对表示卡片的元组列表进行排序,然后排名
【发布时间】:2021-02-07 17:24:17
【问题描述】:

我想先按字母顺序(梅花、钻石、红心、黑桃)对一手牌进行排序,然后再按等级(从 A 到 K)。我有以下(非常冗长且效率低下)的代码,但输出似乎错误。我觉得这是我使用的冒泡排序的问题。我在这里缺少什么,以及这样做的更好方法是什么?对于我的最终输出,我还将把 2、11、12 和 13 转换回 Ace、Jack、Queen 和 King,但我真的不想重做一开始就将它们转换为数值的操作。 I/O 应如下所示:

样本输入:[('h', 'q'), ('c', 'j'), ('d', '2'), ('s', '3'), ('h ', '5'), ('d', 'j'), ('d', '10')]

样本输出:[('c', 'j'), ('d', '2'), ('d', '10'), ('d', 'j'), ('h ', '5'), ('h', 'q'), ('s', '3')]

这是我目前的代码和结果输出:

def sort_hand(hand):
    """
    Sorts the a hand of cards by the value of each suit/type of card. 
    """
    
    # A=1,2,3,4,5,6,7,8,9,10,J=11,Q=12,K=13
    # UPDATING CARDS SO THAT THEY CAN BE ORDERED BY RANK
    new_hand = []
 
    # GIVING NUMERIC VALUES TO JACK, QUEEN, KING, AND ACE
    for element in hand: 
        if element[1] == 'j':
            val = element[1].replace('j', '11')
            new_element = (element[0], val)
            new_hand.append(new_element)
        elif element[1] == 'q':
            val = element[1].replace('q', '12')
            new_element = (element[0], val)
            new_hand.append(new_element)
        elif element[1] == 'k':
            val = element[1].replace('k', '13')
            new_element = (element[0], val)
            new_hand.append(new_element)
        elif element[1] == 'a':
            val = element[1].replace('a', '1')
            new_element = (element[0], val)
            new_hand.append(new_element)
        else:
            pass
            new_hand.append(element)
    
    # BUBBLE SORT USED TO ORDER BY RANK 
    for ix in range(1, len(new_hand)):
        value_to_sort = new_hand[ix][1]

        while new_hand[ix-1][1] > value_to_sort and ix > 0:
            new_hand[ix], new_hand[ix-1] = new_hand[ix-1], new_hand[ix]
            ix -= 1
            
    # MAKE SUBLISTS FOR EACH SUIT      
    c_list = []
    d_list = []
    h_list = []
    s_list = []
            
    for element in new_hand:
        if element[0] == 'c':
            c_list.append(element)
        elif element[0] == 'd':
            d_list.append(element)
        elif element[0] == 'h':
            h_list.append(element)
        else: 
            s_list.append(element)
    
    # COMBINE ORDERED SUIT SUBLISTS TO MAKE FINAL SORTED HAND BY RANK
    final_hand = c_list + d_list + h_list + s_list
            
    return final_hand

>>> hand = [('h', 'q'), ('c', 'j'), ('d', '2'), ('s', '3'), ('h', '5'), ('d', 'j'), ('d', '10')]  
>>> sort_hand(hand)
[('c', '11'),
 ('d', '10'),
 ('d', '11'),
 ('d', '2'),
 ('h', '12'),
 ('h', '5'),
 ('s', '3')]

我觉得我很接近这段代码,但不知道从这里去哪里。希望得到一个解释清楚的答案,谢谢。

【问题讨论】:

  • 一般来说,更新for 循环内的循环变量是个坏主意。不要使用ix 作为while 循环中的循环变量,而是引入一个初始化为jx = ix 的新变量jx,并将ix 替换为jxwhile 循环内的任何地方。

标签: python function sorting tuples bubble-sort


【解决方案1】:

您可以使用内置的sortsorted 函数来实现这一点

您需要使用上述功能的附加功能是比较器逻辑,可以使用key 参数提供。

def sort_hand(inp):
  ##### I have created the order of the suites arbitrarily
  suite_map = {
     'h':1
     ,'c':2
     ,'s':3
     ,'d':4
  }

  rank_map = {
        'a':1
        'k':2
        'q':3
        'j':4
        '10':5
        '9':6
        '8':7
        '7':8
        '6':9
        '5':10
        '4':11
        '3':12
        '2':13
        '1':14
    }
    
    return suite_map[inp[0]],rank_map[inp[1]]

O/P --->

>>> hand
[('h', 'q'), ('c', 'j'), ('d', '2'), ('s', '3'), ('h', '5'), ('d', 'j'), ('d', '10')]
>>> sorted(hand,key=sort_hand)
[('h', 'q'), ('h', '5'), ('c', 'j'), ('s', '3'), ('d', 'j'), ('d', '10'), ('d', '2')]

您可以根据您的顺序控制基于suite_maprank_map的字典顺序并进行相应的排序

【讨论】:

    【解决方案2】:

    使用 lambda 进行双键排序。

    rank = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'j', 'q', 'k', 'a']
    suit = ['c', 'd', 'h', 's']
    
    inp = [('h', 'q'), ('c', 'j'), ('d', '2'), ('s', '3'), ('h', '5'), ('d', 'j'), ('d', '10')]
    
    outp = sorted(inp,key = lambda x: 
        (suit.index(x[0]), rank.index(x[1])))
    
    print(outp)
    

    输出:

    [('c', 'j'), ('d', '2'), ('d', '10'), ('d', 'j'), ('h', '5'), ('h', 'q'), ('s', '3')]
    

    【讨论】:

    • 谢谢!像魅力一样工作。到目前为止,我对 lambda 函数和单键很熟悉,所以你能解释一下双键在这种情况下是如何工作的吗?我通过 (suit.index(x[0]), rank.index(x[1]) 识别出您正在通过它们的套装和等级来提取元组,但是在后台排序是如何工作的?它是第 0 位吗?先索引,然后是第一个索引?
    • 当你使用key=tuple时,它会根据元组中元素的顺序进行排序。 peterbe.com/plog/in-python-you-sort-with-a-tuple
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多