Sort list of lists by length and by value

I have a list of lists:

>>> a = [['3D'], ['3D', '4D', '5D'], ['4C'], ['2C'],['4C', '4D'], ['4D'], ['5D'], \ ... ['JC'], ['JC', 'JS'], ['JS']] 

You may notice that these are card values, i.e. C = Clubs, etc. J = Jack etc. I also have a list of links:

 >>> confrom = {'3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, \ ... '0':10, 'J':11, 'Q':12, 'K':13, 'A':14, '2':15} 

How do I play a card game, where the maximum value is 2. To sort by the length of the list, I do:

 >>> a = sorted(a, key = lambda x: len(x)) >>> a ... [['3D'], ['4C'], ['4D'], ['2C'], ['5D'], ['JC'], ['JS'], ['4C', '4D'], ['JC', 'JS'], ['3D', '4D', '5D']] 

I also need to sort them according to my dictionary value, so my resulting list will look like this:

 >>> [['3D'], ['4C'], ['4D'], ['5D'], ['JC'], ['JS'], ['2C'], ['4C', '4D'], ['JC', 'JS'], ['3D', '4D', '5D']] 

This is currently a fairly simple implementation, but I want to be able to sort it in a more complex way.

+6
source share
2 answers

Try the following:

 sorted(a, key = lambda x: (len(x), [confrom[card[0]] for card in x])) 

ideone

+9
source

This can be a really good example of moving from a basic data structure in Python to a class.

Consider:

 values = ('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A') suits = ('H', 'C', 'D', 'S') sRep = {'H':'Hearts', 'C':'Clubs', 'D':'Diamonds', 'S':'Spades'} ranks = {'2':15, '3':3, '4':4,'5':5,'6':6,'7':7,'8':8, '9':9, '0':10, '0':10, 'J':11, 'Q':12, 'K':13, 'A':14 } class Card: def __init__(self, value, suit): value=str(value) self.value, self.suit = value.upper(), suit.upper() self.rSuit = sRep[suit.upper()] self.rank = ranks[value.upper()] def __repr__(self): return "%s of %s" % (self.value, self.rSuit) def __cmp__(self,other): if self.rank > other.rank: return 1 if self.rank < other.rank: return -1 if self.value > other.value: return 1 if self.value < other.value: return -1 if self.rSuit > other.rSuit: return 1 if self.rSuit < other.rSuit: return -1 return 0 

Try some cards:

 c1=Card(2,'s') c2=Card(4,'d') if c1>c2: print "A", c1, "beats a", c2 elif c2>c1: print "A", c2, "beats a", c1 else: print "Same..." 

Fingerprints:

 A 2 of Spades beats a 4 of Diamonds 

Since we determined the sort order in the class, complex sorting is simple and ranking based on different games is very simple.

Your list of cards as an example:

 a = [['3D'], ['3D', '4D', '5D'], ['4C'], ['2C'],['4C', '4D'], ['4D'], ['5D'], ['JC'], ['JC', 'JS'], ['JS']] print sorted([Card(c[0],c[1]) for e in a for c in e]) 

Print

 [3 of Diamonds, 3 of Diamonds, 4 of Clubs, 4 of Clubs, 4 of Diamonds, 4 of Diamonds, 4 of Diamonds, 5 of Diamonds, 5 of Diamonds, J of Clubs, J of Clubs, J of Spades, J of Spades, 2 of Clubs] 

With a little work you can identify the hands and the fact that the hand beats with the other hand.

You can learn more about this example in the classic Python book. How to Think Like a Computer Scientist: Learning with Python HERE

+2
source

Source: https://habr.com/ru/post/916521/


All Articles