python似乎无法获得正确返回的列表

时间:2015-01-29 01:22:08

标签: python list

   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列表中,并将新手列表附加到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']]

4 个答案:

答案 0 :(得分:1)

我认为在执行代码之后,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

答案 1 :(得分:0)

首先,检查你的缩进 - 很难理解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列表的末尾,并且比在末尾插入元素要清晰得多。

答案 2 :(得分:0)

您甚至无需向hand中的for loop列表添加任何内容。它只会造成不必要的混乱。这似乎可以做你想做的一切。

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):

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`.

答案 3 :(得分:0)

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'])