python 使用空值填充的列表列表列表

bihw5rsg  于 2023-05-05  发布在  Python
关注(0)|答案(1)|浏览(181)

从Java背景学习Python。下面是一个表示扑克牌组的类。在这个类中有一个方法/函数,它将通过将卡片分成x数量的堆栈,然后颠倒顺序,然后合并回来,首先从y堆栈开始。
问题是列表利益相关者的列表没有填充数据,只是填充了空列表...我已经测试了大多数其他的输出/输入,并且看起来像预期的那样运行
对草率和/或过度编码的道歉

def mergeshuffle(self, x, y):
    if x > len(self.cards):
        print("Invalid input. Try again using an integer value 1-52")
        return
    elif x < 0:
        print("Invalid input. Try again using an posotive integer value 1-52")
        return
    
    stacks = int(len(self.cards)/x)
    cardsinstack = int(len(self.cards)/stacks)
    print(str(stacks) +" <- stacks cards in stack -> " + str(cardsinstack))
    counter = 0
    stackholder = []   
       
    for i in range(stacks): 
        thisstack = []               
        for x in range(cardsinstack):
           thisstack.append(self.cards[counter])
           #print (" Added " + str(thisstack[x]))  
           #print(str(counter))
           counter=counter+1
        thisstack.reverse()
        stackholder.append(thisstack)
        thisstack.clear()
    print(str(stackholder))
    #print((str(stackholder)).center(10, '&') )

相关输出(Relative Output)

The deck added 2 of Spades
The deck added 3 of Spades
The deck added 4 of Spades
The deck added 5 of Spades
The deck added 6 of Spades
The deck added 7 of Spades
The deck added 8 of Spades
The deck added 9 of Spades
The deck added 10 of Spades
The deck added Jack of Spades
The deck added Queen of Spades
The deck added King of Spades
The deck added Ace of Spades
The deck added 2 of Clubs
The deck added 3 of Clubs
The deck added 4 of Clubs
The deck added 5 of Clubs
The deck added 6 of Clubs
The deck added 7 of Clubs
The deck added 8 of Clubs
The deck added 9 of Clubs
The deck added 10 of Clubs
The deck added Jack of Clubs
The deck added Queen of Clubs
The deck added King of Clubs
The deck added Ace of Clubs
The deck added 2 of Diamonds
The deck added 3 of Diamonds
The deck added 4 of Diamonds
The deck added 5 of Diamonds
The deck added 6 of Diamonds
The deck added 7 of Diamonds
The deck added 8 of Diamonds
The deck added 9 of Diamonds
The deck added 10 of Diamonds
The deck added Jack of Diamonds
The deck added Queen of Diamonds
The deck added King of Diamonds
The deck added Ace of Diamonds
The deck added 2 of Hearts
The deck added 3 of Hearts
The deck added 4 of Hearts
The deck added 5 of Hearts
The deck added 6 of Hearts
The deck added 7 of Hearts
The deck added 8 of Hearts
The deck added 9 of Hearts
The deck added 10 of Hearts
The deck added Jack of Hearts
The deck added Queen of Hearts
The deck added King of Hearts
The deck added Ace of Hearts
52 playing cards total.
13 <- stacks cards in stack -> 4
[[], [], [], [], [], [], [], [], [], [], [], [], []]

希望列表的列表将填充有len() = 4的列表,该len() = 4的列表包含如所打印的顺序地以4为一组的上述卡。
我试着测试程序中的其他变量,看看它是如何运行的,就我所做的研究而言,它似乎可以正常运行,就像一些注解的打印语句所显示的那样。

o2gm4chl

o2gm4chl1#

thisstack.clear()

在Python中,列表是可变的。这意味着您可以将一个列表追加到一个列表,然后清除内部列表。通过清除此列表,即使该列表附加到另一个列表后,也可以清除该列表中的列表。
您应该创建一个新列表,而不是清除列表。你可以这样做:

thisstack = []

但是,您已经在循环的顶部这样做了,所以您可以删除thisstack.clear()行。

相关问题