簡體   English   中英

python似乎無法獲得正確返回的列表

[英]python can't seem to get a list to return properly

   def best_wild_hand(hand):

    dictSuit = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':11, 'Q':12, 'K':13, 'A':14 }
    listofLists = []

    blackJoker = "?B"


    list1 = [x + "S" for x in dictSuit]
    index = len(hand)

    if blackJoker in hand:

        newHand = hand
        newHand.remove(blackJoker)
        for d in list1:
            newHand.insert(index +1, d)
            listofLists.append(newHand)
        return listofLists




    print best_wild_hand(['6C', '7C', '8C', '9C', 'TC', '5C', '?B'])

我的輸出應該在列表的格式列表中。 我的代碼似乎將清單中的每個元素都給了newHand(也是清單)。 我只希望將一個元素插入到newHand列表中,並將newhand列表附加到listofLists中。 我在下面格式化了

所需的清單輸出清單

[['6C', '7C', '8C', '9C', 'TC', '5C', '2S']
 ['6C', '7C', '8C', '9C', 'TC', '5C', '3S']
 ['6C', '7C', '8C', '9C', 'TC', '5C', '4S']
 ['6C', '7C', '8C', '9C', 'TC', '5C', '5S']
 .
 .
 .
 ....................................'14S']]

我認為執行代碼后,listofLists將是:

[newHand, newHand, newHand, ..., newHand]

但是newHand每次循環處理時都會更改,最后,listOfList將包含許多相同的newHand。 您可以這樣編寫循環塊:

if blackJoker in hand:
    curHand = hand
    curHand.remove(blackJoker)
    for d in list1:
        newHand = curHand[:-1]
        newHand.insert(index +1, d)
        print newHand
        listofLists.append(newHand)
    return listofLists

首先,檢查您的縮進-很難理解if和for中的內容。 關於您的目標,這是我的最佳猜測:

def best_wild_hand(hand):
    dictSuit = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':11, 'Q':12, 'K':13, 'A':14}
    result = []

    blackJoker = "?B"

    cardList = [x + "S" for x in dictSuit]
    index = len(hand)

    if blackJoker in hand:
        hand.remove(blackJoker)
        for card in cardList:
            result.append(hand + [card])
    return result

append可用於將一個元素放在python列表的末尾,並且比在末尾插入元素要干凈得多。

您甚至不需要在for loop中將任何內容添加hand列表中。 這只會造成不必要的混亂。 這似乎可以滿足您的所有需求。

def best_wild_hand(hand):
    dictSuit = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':11, 'Q':12, 'K':13, 'A':14}

    blackJoker = "?B"
    cards = [x + "S" for x in dictSuit]

    finalList = []
    if blackJoker in hand:
        hand.remove(blackJoker)
        for c in cards:
            finalList.append(hand + [c])
    return finalList

您也可以將其作為列表 finalList來完成(並通過立即返回結果來跳過對finalList的初始化):

if blackJoker in hand:
    hand.remove(blackJoker)
    return [hand + [c] for c in cards]

請記住,類似newHand = hand代碼(其中hand是列表)不會創建原始列表的副本。 因此,這在Python中是相當無用的分配,因為修改newHand還將修改hand引用的原始數據,因為您正在處理可變對象( list )。

如果您不想修改原始列表,則可以通過以下方式進行復制:

import copy

# Now when you modify newHand (i.e appending or removing items) 
# the original hand list from the caller's frame will not be affected.
newHand = copy.copy(hand)

# You can alternatively copy the list using...
newHand = hand[:]

# However, if there are other mutable objects inside of `hand` you wished
# to copy, you'd have to use the `deepcopy` method of `copy`.
def best_wild_hand(hand):

    dictSuit = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':11, 'Q':12, 'K':13, 'A':14 }
    blackJoker = "?B"
    if blackJoker in hand:
        return [[x if x != blackJoker else str(i) + "S" for x in hand ] for i in range(2, 15)]


print best_wild_hand(['6C', '7C', '8C', '9C', 'TC', '5C', '?B'])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM