开发者

Problem sorting list of strings - Python

I have a list of strings:

cards = ['2S', '8D', '8C', '4C', 'TS', '9S', '9D', '9C', 'AC', '3D']

and the order in which I want to display the cards:

CARD_ORDER = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']

This is how I'm trying to 开发者_开发问答order the list:

sorted(cards, lambda x,y: CARD_ORDER.index(x[0]) >= CARD_ORDER.index(y[0]) )

Unfortunately this does not seem to work....

or more precisely the list stays exactly the same, sorted(cards) works fine instead.

Any ideas?


it's

sorted(cards, key=lambda x: CARD_ORDER.index(x[0]))

key parameter accepts a single value, by which to sort the main iterable. You're probably trying to use cmp parameter which is not recommended for quite some time.


Try

sorted(cards, key = lambda x: CARD_ORDER.index(x[0]) )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜