Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
列表排序的Python纸牌游戏_Python_Python 3.x_List - Fatal编程技术网

列表排序的Python纸牌游戏

列表排序的Python纸牌游戏,python,python-3.x,list,Python,Python 3.x,List,我有一个纸牌游戏,玩家的手上有一张从上到下的纸牌列表,我需要检查纸牌列表从小到大(从上到下)的顺序。我不能使用任何类型的GUI 比如说,, 玩家手中的牌: 23 24 12 5-检查列表中正确排序的第五个元素 4. 3. 2. 1出于解释原因对代码进行了注释,hth: cardsInHand = [23,24,25,23,27,4] # all your cards cardsInOrder = [] # empty list, to be filled wi

我有一个纸牌游戏,玩家的手上有一张从上到下的纸牌列表,我需要检查纸牌列表从小到大(从上到下)的顺序。我不能使用任何类型的GUI

比如说,, 玩家手中的牌: 23 24 12 5-检查列表中正确排序的第五个元素 4. 3. 2.
1

出于解释原因对代码进行了注释,hth:

cardsInHand = [23,24,25,23,27,4]  # all your cards

cardsInOrder = []                 # empty list, to be filled with in order cards
lastCard = None  

for card in cardsInHand:                  # loop all cards in hand
    if not lastCard or lastCard < card:       # if none taken yet or smaller
        cardsInOrder.append(card)                 # append to result
        lastCard = card                           # remember for comparison to next card
    else:                                     # not in order
        break                                     # stop collecting more cards into list 

print(cardsInOrder)               # print all
如果您还需要手的无序部分,您可以通过以下方式获得:

unorderedCards = cardsInHand[len(cardsInOrder):] # list-comp based length of ordered cards

那么,让我们看看迄今为止您尝试了什么如果hands1[0]>hands1[1]:solitaireScore=5如果hands1[1]>hands1[2]:solitaireScore=10但问题是,我试图将其设置为嵌套的if语句,但不知道如何实现。我想检查第一张卡是否大于第二张卡,如果这是真的,则继续。对于其他条件,你可以考虑使用字典“{ 23”:4,“24”:3,“12”:2,“5”:1 },然后如果使用排序(YouXDICT)-像<代码>排序(YouDist.IdItItMss.),key = lambda(k,v):(v,k) -使用字典将给你O(1)时间,VS使用list@dmitryro但是排序本身也是
O(nlogn)
,您能帮我解决写入列表的问题吗?每次我退出文件并添加更多信息时,它都会删除以前的数据?@SimeonGeorge关于python上的“写入文件”有很多问题,因此:举个例子。搜索他们。或者在这里阅读:
unorderedCards = cardsInHand[len(cardsInOrder):] # list-comp based length of ordered cards