Returning 5 best cards containing a pair from a set of 7 playing cards using Python
Question: I'm currently trying to use Python's Enum module on Python 2.7 to identify
pair of cards from a hand of 7

    
    
    import collections
    import operator
    import enum
    
    Card = collections.namedtuple("Card", "rank suit")
    #in the below hand there is a pair of fours. 
    hand = [
        Card(rank=<Ranks.four: 3>, suit=<Suits.spades: 1>),
        Card(rank=<Ranks.nine: 8>, suit=<Suits.clubs: 3>),
        Card(rank=<Ranks.ten: 9>, suit=<Suits.spades: 1>),
        Card(rank=<Ranks.jack: 10>, suit=<Suits.diamonds: 4>),
        Card(rank=<Ranks.six: 5>, suit=<Suits.hearts: 2>),
        Card(rank=<Ranks.four: 3>, suit=<Suits.diamonds: 4>),
        Card(rank=<Ranks.two: 1>, suit=<Suits.clubs: 3>),
        ]
    
    #My function
    def is_pair():
        #count duplicate-numbers in `hand`
        ranks = collections.Counter(map(operator.attrgetter("rank"), hand))
        pair_card=[]
        if len(ranks) == 6:
            # get most common if there are individual counts
            # (so one is duplicated and not counted)
            pair_card = ranks.most_common(1)[0]6
        for i in hand:
            print i
        print pair_card
        print type(pair_card)
    

The above code will recognize a pair but I want it to return the 5 best cards,
which would be the pair of cards plus the three highest (as per the rules of
Poker). So my question is how can I get the above function to return any pair
from any 7 cards, along with the other three highest cards?

So in this case, the desired output is:

    
    
    output = [
        Card(rank=<Ranks.four: 3>, suit=<Suits.spades: 1>),
        Card(rank=<Ranks.nine: 8>, suit=<Suits.clubs: 3>),
        Card(rank=<Ranks.ten: 9>, suit=<Suits.spades: 1>),
        Card(rank=<Ranks.jack: 10>, suit=<Suits.diamonds: 4>),
        Card(rank=<Ranks.four: 3>, suit=<Suits.diamonds: 4>),
        ]
    

That's to say, removal of the 2 of clubs and 6 of hearts.


Answer: Is your Rank enum also a subclass of `int`?

If so, just order the remainng cards and take the last three.

If not, add ordering:

    
    
    def __lt__(self, other):
        if not isinstance(other, Rank):  # or self.__class__ instead of Rank
            return NotImplemented
        return self.value < other.value
    

