扑克队伍按此顺序进行。 '23456789TJQKA',其中T是10,然后是杰克皇后国王和Ace,Ace是最高的牌。输入一个字符串,并且始终以5个字符串输入。
如果用户输入说字符串='43TKQ',那么只有在语句,while和for循环的情况下,如何才能在没有任何高级方法的情况下在此字符串中找到最高的卡。我尝试了max(string)但它返回十而不是King
hand = '28TKQ'
print(max(hand)) #This outputs T (it should output K)
'768TA'应打印A
'2TJ67'应该打印J
参考方案
您需要指定Order逻辑女巫为23456789TJQKA
:
order = '23456789TJQKA'
print(max('28TKQ', key=lambda e: order.index(e))) # K
print(max('A43TKQ', key=lambda e: order.index(e))) # A
另一个pythonic代码:
cards = '23456789TJQKA'
print(max('28TKQ', key=cards.index)) # K
print(max('A43TKQ', key=cards.index)) # A
编辑
要回答您的评论:
当您在列表元素中搜索max
时,您将比较项目,默认情况下,str
与Unicode code point
进行比较,因此,当您比较列表28TKQ
'T'
的元素时,其最大值84
,,这就是为什么ord('T') > ord('K')
返回max
而不是T
的原因,但是您不需要这种压缩逻辑告诉K
在比较中需要不同的逻辑。
这就是为什么我们使用传递方法的max
参数,并且根据该方法的返回值对项目进行排序的原因。
编辑:
为您清除更多东西
# 2 priority is 0, ....., A priority is 12
card_priority = '23456789TJQKA'
def get_card_priority(card):
""" return the priority of card, it's index in card_priority list"""
return card_priority.index(card)
# compare get_card_priority('2') = 0, get_card_priority('8') = 6,get_card_priority('T')=8.......get_card_priority('Q') = 10
# The element with the highest value is returned witch is 'K' = 11
print(max('28TKQ', key=get_card_priority)) # K because get_card_priority('K') return the biggest value
在返回'Response'(Python)中传递多个参数 - python我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…
Python exchangelib在子文件夹中读取邮件 - python我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…
python JSON对象必须是str,bytes或bytearray,而不是'dict - python在Python 3中,要加载以前保存的json,如下所示:json.dumps(dictionary)输出是这样的{"('Hello',)": 6, "('Hi',)": 5}当我使用json.loads({"('Hello',)": 6,…
R'relaimpo'软件包的Python端口 - python我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。
Python ThreadPoolExecutor抑制异常 - pythonfrom concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…