简体   繁体   English

如何按字母顺序倒序排序列表但首先是数字

[英]How to sort a list alphabetically in reverse but numbers first

I'm coding a simple blackjack program they way I coded I need "A" to be sorted to the end of the list so it must be reversed alphabetically and all the numbers should be checked before it我正在编写一个简单的二十一点程序,他们按照我的方式编码我需要将“A”排序到列表的末尾,因此必须按字母顺序颠倒它,并且应该在它之前检查所有数字

revelant part of the code:代码的相关部分:

oyuncukart = []
eroskart = []
oyuncuToplam = 0
erosToplam = 0

def Donustur(El,oyuncuTop):
    aTane = El.count("A")
    for kart in El:
        if "2" == kart:
            oyuncuTop += 2
        if "3" == kart:
            oyuncuTop += 3
        if "4" == kart:
            oyuncuTop += 4
        if "5" == kart:
            oyuncuTop += 5
        if "6" == kart:
            oyuncuTop += 6
        if "7" == kart:
            oyuncuTop += 7
        if "8" == kart:
            oyuncuTop += 8
        if "9" == kart:
            oyuncuTop += 9
        if "10" == kart:
            oyuncuTop += 10
        if "J" == kart:
            oyuncuTop += 10
        if "Q" == kart:
            oyuncuTop += 10
        if "K" == kart:
            oyuncuTop += 10
        if "A" == kart and aTane == 1 and oyuncuTop <= 10:
            oyuncuTop += 11
        elif "A" == kart and aTane == 1 and oyuncuTop > 10:
            oyuncuTop += 1
        elif "A" == kart and aTane > 1  and oyuncuTop < 10:
            oyuncuTop += 11
        elif "A" == kart and aTane > 1 and oyuncuTop >= 10:
            oyuncuTop += 1

    return oyuncuTop

kartlar = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]


oyuncukart.append(random.choice(kartlar))
oyuncukart.append(random.choice(kartlar))
oyuncukart.sort(reverse=True)

You don't want to sort numerically or alphabetically (reversed or not);您不想按数字字母顺序排序(颠倒或不颠倒); you want the Very Special sort order 2,3,4,5,6,7,8,9,J,Q,K,A .你想要非常特殊的排序顺序2,3,4,5,6,7,8,9,J,Q,K,A

You can apply a custom order to sort inside the command itself:您可以应用自定义顺序在命令本身内部进行sort

oyuncukart.sort(key=["2","3","4","5","6","7","8","9","10","J","Q","K","A"].index)

and this might produce (with more draws than 2 -- but it's only about the sort order so that doesn't matter) a sequence such as this:这可能会产生(抽签次数多于 2 次——但这只是排序顺序,所以无关紧要)如下所示:

['2', '10', 'J', 'Q', 'A']

I would approach this in a different way, with a function that returns the value of the card that can be used as a key argument to sorted() .我会以不同的方式解决这个问题,使用一个函数返回卡片的值,该值可用作sorted()key参数。 That would give us the following:这会给我们以下内容:

Code:代码:

face_cards = {'J': 11,
              'Q': 12,
              'K': 13,
              'A': 14}

def card_val(card):
    try:
        return int(card)
    except ValueError:
        return face_cards[card]

kartlar = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]

sorted_kartlar = sorted(kartlar, key=card_val)

Usage:用法:

>>> import random
>>> kartlar = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]
>>> random.shuffle(kartlar)
>>> kartlar
['6', '4', '10', 'J', '2', 'A', 'K', '8', '3', '5', 'Q', '9', '7']
>>> sorted(kartlar, key=card_val)
['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM